javascript - Regexp (?<=string) is giving invalid group error? -
this question has answer here:
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
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
Post a Comment