0
var rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/;
var str = ' <div id=top></div>';
var match = rquickExpr.exec(str);
console.log(match);

then i get

[" <div id=top></div>", "<div id=top></div>", undefined, index: 0, input: " <div id=top></div>"]

why there is undefined?

  • The second group did not match. You can see it [here](https://regex101.com/r/lZ7lH7/1) (when enabling the SHOW NON-PARTICIAPTING GROUPS in the *Options*). What is that you really need? Is the regex working as expected or not? Before using the match results, just check `if (m[2])` and then use it. – Wiktor Stribiżew Sep 23 '16 at 08:29
  • It's not a *jQuery* regex. It's a *JavaScript* regex. jQuery is a library of DOM manipulation functions, not a language, and has nothing to do with regular expressions. – T.J. Crowder Sep 23 '16 at 08:30
  • And...[obligatory link about trying to parse HTML with regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags#1732454). – T.J. Crowder Sep 23 '16 at 08:31

0 Answers0