开发者

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

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:

\[+[0-9]+\]

This returns [7], however I want to return [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:开发者_Go百科

(?:.*?(\[+[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.

Cheers!


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

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

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

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


Try

result = subject.replace(/(\[\d\]\[[^\]]+\])\[\d\]/, "$1[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

result = subject.replace(/(\[\d\](?:\[[^\]]+\])*?)\[\d\]/, "$1[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.


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

returns:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜