XRegExp:“不匹配')'"然而,一切似乎都是平衡的 [英] XRegExp: "Unmatched ')'" Yet everything appears to be balanced

查看:131
本文介绍了XRegExp:“不匹配')'"然而,一切似乎都是平衡的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码行:

XRegExp.exec(data,
    XRegExp('<!-- @\[(?<component>[\w+])[\(*(?<classes>[\w+])*\)]*\] -->', 'g'))

您可以看到它正在使用 XRegExp 库,因此我可以使用命名组和其他PCRE功能.

As you can see it is using the XRegExp library so I can use named groups other PCRE features.

我遇到了错误:

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)] -->

这些还应该匹配:

<!-- @[NoteBlock(warning, high-level)] -->

<!-- @[NoteBlock] -->

这不应该匹配:

<!-- @[(warning, high-level)] -->

UDPATE:使用 Regex101 我设法使正则表达式通过:已将正则表达式更新为'<!-- @\[(?<component>[\w+]+)\(*(?<classes>[\w+]+)*\)*\] -->',但是我仍然遇到相同的错误.

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.

推荐答案

我已经更新了答案,以满足提供的新信息.

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.

以下内容将匹配[[到component之间的任何字符,以及()到class之间的任何字符.

The following will match any characters between [( to component and anything between () to classes.

\ w匹配单词,数字或下划线. 这会使它缺少您的[-,]字符

\w matches a word, digit, or underscore. This would leave it missing your [-,] characters

希望这会有所帮助.

<!-- @\[(?<component>[\w]*)\((?<classes>.*?)\)*\] -->

@ [Component(类-警告,高级别)]

@[Component(Classes - warning, highlevel)]

这篇关于XRegExp:“不匹配')'"然而,一切似乎都是平衡的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆