XRegExp: “Unmatched ')'” Yet everything appears to be balanced [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-11 04:26:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!