Question

I am trying to build a list of urls contained in an xml document where I am matching everything inside of <id>http://xxx.xxxx.com</id> and I'm having trouble.

I'm trying to match this pattern: /(?<id>)http://(.*?).com(?=</id>)/g

It works on regexr.com but I cannot seem to get it to work with my jsfunction:

var regEx = new RegExp('/(?&lt;id>)http://(.*?).com(?=&lt;/id>)/g');

I am getting an error:

SyntaxError: Invalid regular expression: //(?<id>)http://(.*?).com(?=</id>)/g/: Invalid group

Was it helpful?

Solution

Try this regex:

 (?!<id>)http:\/\/(.*?)\.com(?=<\/id>)

Instead of:

 (?<id>)http://(.*?).com(?=</id>)

see demo here: http://regex101.com/r/xH8mH7

OTHER TIPS

Try DOMParser ;

// Code tested under Firefox 27
var doc = new DOMParser()
    .parseFromString('<id>http://xxx.xxxx.com</id>', "application/xml");
var ids = doc.querySelectorAll('id'); // Return: NodeList[id]

However it doesn't work in IE <= 8 (Browser compatibility)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top