Example string: $${a},{s$${d}$$}$$
示例字符串:$$ {a},{s $$ {d} $$} $$
I'd like to match $${d}$$
first and replace it some text so the string would become $${a},{sd}$$
, then $${a},{sd}$$
will be matched.
我想首先匹配$$ {d} $$并将其替换为一些文本,以便字符串变为$$ {a},{sd} $$,然后是$$ {a},{sd} $$将是匹配。
5 个解决方案
#1
22
Annoyingly, Javascript does not provide the PCRE recursive parameter (?R)
, so it is far from easy to deal with the nested issue. It can be done however.
令人讨厌的是,Javascript没有提供PCRE递归参数(?R),因此处理嵌套问题并不容易。然而,它可以做到。
I won't reproduce code, but if you check out Steve Levithan's blog, he has some good articles on the subject. He should do, he is probably the leading authority on RegExp in JS. He wrote XRegExp, which replaces most of the PCRE bits that are missing, there is even a Match Recursive plugin!
我不会重现代码,但如果你查看Steve Levithan的博客,他会有一些关于这个主题的好文章。他应该这样做,他可能是JS中RegExp的主要权威。他编写了XRegExp,它取代了大多数缺少的PCRE位,甚至还有一个Match Recursive插件!
#2
1
Since you want to do this recursively, you are probably best off doing multiple matches using a loop.
由于您希望以递归方式执行此操作,因此最好使用循环执行多个匹配。
Regex itself is not well suited for recursive-anything.
正则表达式本身并不适合递归任何东西。
#3
0
In general, Regexps are not well suited for that kind of problem. It's better to use state machine.
通常,Regexp不适合这种问题。最好使用状态机。
#4
0
var content = "your string content";
var found = true;
while (found) {
found = false;
content = content.replace(/regex/, () => { found = true; return "new value"; });
}
#5
0
I wrote this myself:
我自己写了这个:
String.prototype.replacerec = function (pattern, what) {
var newstr = this.replace(pattern, what);
if (newstr == this)
return newstr;
return newstr.replace(pattern, what);
};
Usage:
用法:
"My text".replacerec(/pattern/g,"what");
P.S: As suggested by @lededje, when using this function in production it's good to have a limiting counter to avoid stack overflow.
P.S:正如@lededje所建议的那样,在生产中使用这个功能时,最好有一个限制计数器以避免堆栈溢出。
#1
22
Annoyingly, Javascript does not provide the PCRE recursive parameter (?R)
, so it is far from easy to deal with the nested issue. It can be done however.
令人讨厌的是,Javascript没有提供PCRE递归参数(?R),因此处理嵌套问题并不容易。然而,它可以做到。
I won't reproduce code, but if you check out Steve Levithan's blog, he has some good articles on the subject. He should do, he is probably the leading authority on RegExp in JS. He wrote XRegExp, which replaces most of the PCRE bits that are missing, there is even a Match Recursive plugin!
我不会重现代码,但如果你查看Steve Levithan的博客,他会有一些关于这个主题的好文章。他应该这样做,他可能是JS中RegExp的主要权威。他编写了XRegExp,它取代了大多数缺少的PCRE位,甚至还有一个Match Recursive插件!
#2
1
Since you want to do this recursively, you are probably best off doing multiple matches using a loop.
由于您希望以递归方式执行此操作,因此最好使用循环执行多个匹配。
Regex itself is not well suited for recursive-anything.
正则表达式本身并不适合递归任何东西。
#3
0
In general, Regexps are not well suited for that kind of problem. It's better to use state machine.
通常,Regexp不适合这种问题。最好使用状态机。
#4
0
var content = "your string content";
var found = true;
while (found) {
found = false;
content = content.replace(/regex/, () => { found = true; return "new value"; });
}
#5
0
I wrote this myself:
我自己写了这个:
String.prototype.replacerec = function (pattern, what) {
var newstr = this.replace(pattern, what);
if (newstr == this)
return newstr;
return newstr.replace(pattern, what);
};
Usage:
用法:
"My text".replacerec(/pattern/g,"what");
P.S: As suggested by @lededje, when using this function in production it's good to have a limiting counter to avoid stack overflow.
P.S:正如@lededje所建议的那样,在生产中使用这个功能时,最好有一个限制计数器以避免堆栈溢出。