This regular expression throws an error in JavaScript: <li id=\"[^\"]*+\"
I have a bunch of LI elements for which I need to change the ID names of.
Example target text I would search:
<li id="apple">blah blah blah</li>
<other stuff>
<li id="bacon">blah blah blah</li>
I would want to find just the part that says:
<li id="idnamehere"
I tried using this line of code in JavaScript:
myRegExp = new RegExp('<li id="[^"]*+"', "g");
However I get this error:
"SyntaxError: Invalid regular expression: nothing to repeat"
When I try this expression in a regular expression editor, it works f开发者_开发知识库ine. Any ideas as to what's going wrong?
JavaScript doesn't support possessive quantifiers, therefore [^"]*+
is illegal. Drop the +
, then it should work.
there is no entity for the +
to repeat. decide if you want to use +
or *
, i.e. (i removed the escapes)
<li id="[^"]+"
to match one-or-more-character-ids or
<li id="[^"]*"
to match empty id, too. furthermore you don’t need explicit lazy or greedy matching if you use the unambiguous way you did: *?
and *
mean the same in your case, i.e. “match everything until you encounter an quotation mark.”
but generally: don’t parse XML with regular expressions, as the XML parsers built in your browser normally do a better job than you.
精彩评论