javascript - Regexp (?<=string) is giving invalid group error? -


i'm trying create regular expression match string <tag>something</tag> , want result return something without tags.

i tried using:

string.match(/(?<=<tag>).*?(?=<\/tag>)/g); 

but giving error:

syntaxerror:invalid regular expression: /(?<=<tag>).*?(?=<\/tag>)/: invalid group;

why not working?

i think this

/(?:<(tag)>)((?:.(?!<\/\1>))+.)(?:<\/\1>)/g 

regular expression visualization

visualize match, luke!

this handy because \1 backreference matches tag pairs


use on text this

var re  = /(?:<(tag)>)((?:.(?!<\/\1>))+.)(?:<\/\1>)/g,     str = "this <tag>some text</tag> , <tag>matching</tag>",     match;  while ((match = re.exec(str)) !== null) {   console.log(match[1], match[2]); }; 

output

tag text tag matching 

bonus soda! can modify (tag) (tag|bonus|soda) have work on string

<tag>yay</tag> , there's <bonus>sodas</bonus> in <soda>cans</soda> 

beware if nest tags, have apply regexp recursively.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -