javascript Regex(正则表达式),替换javascript中的第二次出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4641670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:23:51  来源:igfitidea点击:

Regex (regular expressions), replace the second occurence in javascript

javascriptregexstringreplacepattern-matching

提问by MintDeparture

This is an example of the string that's being worked with:

这是正在使用的字符串的示例:

xxxxxx[xxxxxx][7][xxxxxx][9][xxxxxx]

I'm having a little trouble matching the second occurrence of a match, I want to return the 2nd square brackets with a number inside. I have some regex finding the first square backets with numbers in a string:

我在匹配第二次匹配时遇到了一些麻烦,我想返回带有数字的第二个方括号。我有一些正则表达式在字符串中找到第一个带有数字的方形 backets:

\[+[0-9]+\]

This returns [7], however I want to return [9].

这将返回 [7],但是我想返回 [9]。

I'm using Javascript's replace function, the following regex matches the second occurrence (the [9]) in regex testeing apps, however it isn't replaced correctly in the Javascript replace function:

我正在使用 Javascript 的替换功能,以下正则表达式与正则表达式测试应用程序中的第二次出现([9])相匹配,但是它在 Javascript 替换功能中没有被正确替换:

(?:.*?(\[+[0-9]+\])){2}

My question is how do I use the above regex to replace the [9] in Javasctipt or is there another regex that matches the second occurrence of a number in square brackets.

我的问题是如何使用上述正则表达式替换 Javasctipt 中的 [9] 或者是否有另一个正则表达式匹配方括号中数字的第二次出现。

Cheers!

干杯!

采纳答案by moinudin

If xxxis just any string, and not necessarily a number, then this might be what you want:

如果xxx只是任何字符串,而不一定是数字,那么这可能就是您想要的:

(\[[0-9]+\]\[.*?\])\[([0-9]+)\]

This looks for the second number in []. Replace it with $1[<replacement>]. Play with it on rubular.

这将在 中查找第二个数字[]。将其替换为$1[<replacement>]. 在 rubular 上玩它

Your regular expression fails to work as intended because groups followed by +only end up holding the last[xxx].

您的正则表达式无法按预期工作,因为后面的组+只保留最后一个[xxx].

回答by Tim Pietzcker

Try

尝试

result = subject.replace(/(\[\d\]\[[^\]]+\])\[\d\]/, "[replace]");

As a commented regex:

作为评论的正则表达式:

(       # capture the following in backref 1:
\[\d\]  # first occurrence of [digit]
\[      # [
 [^\]]+ # any sequence of characters except ]
\]      # ]
)       # end of capturing group
\[\d\]  # match the second occurence of [digit]

If the number of [xxx]groups between the first and second [digit]group is variable, then use

如果[xxx]第一[digit]组和第二组之间的组数是可变的,则使用

result = subject.replace(/(\[\d\](?:\[[^\]]+\])*?)\[\d\]/, "[replace]");

By surrounding the part that matches the [xxx]groups with (non-capturing) parentheses and the lazy quantifier *?I'm asking the regex engine to match as few of those groups as possible, but as many as necessary so the next group is a [digit]group.

通过[xxx]用(非捕获)括号和惰性量词包围匹配组的部分,*?我要求正则表达式引擎匹配尽可能少的组,但尽可能多,所以下一组是一个[digit]组。

回答by Steven de Salas

console.log( "xxxxxx[xxxxxx][7][xxxxxx][9][xxxxxx]".replace(
/^(.*\[[0-9]+\].*)(\[[0-9]+\])(.*)$/, 
'[15]')); // replace with matches before () and after () your match ()

returns:

返回:

// xxxxxx[xxxxxx][7][xxxxxx][15][xxxxxx]

It will match where [n] is preceeded by 1 set of brackets with numbers inside.

它将匹配 [n] 前面是 1 组括号内的数字。