使用javascript我需要从字符串中获取文本和标记,如:
"type":"homePhone","means":"$[createJohnRequest.contactInfo[type=homePhone].means]"
这样正则表达式将返回:
$[createJohnRequest.contactInfo[type=homePhone].means]
我有几次尝试,但没有一个可行:
/(\$\[(.*?]))/g
将返回:$ [createJohnRequest.contactInfo [type = homePhone]
/(\$\[(.*]))/g
这适用于上述情况,但对于像以下情况一样过于贪心:
{"firstName":"$[user.firstName]","userName":"$[user.username1]","details":
{"description":"this is $[user.username1] the $[user.username2] text th $[user.username3]
at conta$[user.username4]ins the terms we want to find. $[final.object]"}}
理想情况下,我希望单个正则表达式在多行文本中匹配:
some text here $[some.value.here]bunch of noisy text in between here
some more text here$[some.value[index]goes.here]some more noise here
$ [some.value.here]和$ [some.value [index] .goes.here]
任何人都有任何想法指出我正确的方向吗?
我倾向于使用$ [some token] $而不是捕获它。
答案 0 :(得分:0)
你想要这样的东西,只要括号嵌套级别限制为两个:
/\$\[(\[.*]|.)*?\]/g
英文:
a dollar sign followed by
a bracketed sequence whose content is
any number of occurrences of
either
a bracketed subsequence whose content is anything
or something else
如果你想支持更深层次的嵌套,我建议你写一些函数来为你构建正则表达式,这样你就不会疯狂:
function make_regexp(level) {
var build = "\\[.*?]";
while (level--) {
build = "(\\[" + build + "]|.)*?";
}
return RegExp("\\$" + build, "g");
}
> make_regexp(3)
/\$(\[(\[(\[.*]|.*)*?]|.*)*?]|.*)*?/g