问题
I've written the following line of code:
XRegExp.exec(data,
XRegExp('<!-- @\[(?<component>[\w+])[\(*(?<classes>[\w+])*\)]*\] -->', 'g'))
As you can see it is using the XRegExp library so I can use named groups other PCRE features.
I'm getting the error:
syntaxError: Invalid regular expression: /<!-- @[(?<component>[w+])[(*(?<classes>[w+])*)]*] -->/: Unmatched ')'
However, I don't understand where the unmatched )
is. As far as I can tell, all brackets that need to be escaped should be and all the ones that shouldn't are not.
Here is the string I am trying to match:
<!-- @[NoteBlock(warning)] -->
these should also match:
<!-- @[NoteBlock(warning, high-level)] -->
<!-- @[NoteBlock] -->
This should not match:
<!-- @[(warning, high-level)] -->
UDPATE: Using Regex101 I've managed to get the regex to pass: I've updated the regex to '<!-- @\[(?<component>[\w+]+)\(*(?<classes>[\w+]+)*\)*\] -->'
however I'm still getting the same error.
回答1:
I've updated my answer to meet the new information provided.
Given the example of what you're trying to capture the original regex won't match.
The following will match any characters between [( to component and anything between () to classes.
\w matches a word, digit, or underscore. This would leave it missing your [-,] characters
Hopefully this helps.
<!-- @\[(?<component>[\w]*)\((?<classes>.*?)\)*\] -->
@[Component(Classes - warning, highlevel)]
来源:https://stackoverflow.com/questions/53171288/xregexp-unmatched-yet-everything-appears-to-be-balanced