阅读背景:

使用回溯引用的最佳方式是什么?

来源:互联网 

In some

In some \1 will work and in some $1 in online regex builder.

在一些\1将工作,在一些$1的在线regex生成器。

Does both way works in every javascript engine?

这两种方法在每个javascript引擎中都有效吗?

2 个解决方案

#1


3  

Only one way works in JavaScript, and it differs if the back-reference is in the regular expression (\n) or the replacement value ($n).

只有一种方法可以在JavaScript中工作,如果反向引用在正则表达式(\n)或替换值($n)中,则会有所不同。

See MDN Regular Expressions:

看到MDN正则表达式:

[In a regular expression] where n is a positive integer, [\n refers to] a back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).

[在正则表达式中]当n是一个正整数时,[\n指]一个与正则表达式中n个括号相匹配的最后子字符串的后置引用(计数左括号)。

For example, /apple(,)\sorange\1/ matches 'apple, orange,' in "apple, orange, cherry, peach."

例如,/apple()\sorange\1/匹配“apple, orange”中的“apple, orange, cherry, peach”。

..

. .

The \1 and \2 in the pattern match the string's last two words. Note that \1, \2, \n are used in the matching part of the regex. In the replacement part of a regex the syntax $1, $2, $n must be used, e.g.: 'bar foo'.replace( /(...) (...)/, '$2 $1').

模式中的\1和\2匹配字符串的最后两个单词。注意,regex的匹配部分使用了\1、\2、\n。在regex的替换部分,语法必须使用$1、$2、$n,例如:“bar foo”。替换(/(…)(…)/,“$2 $1”)。

That way is the correct way in JavaScript; different regular expressions may work differently, but such is irrelevant to JavaScript code which works as stated above.

这种方法在JavaScript中是正确的;不同的正则表达式可能有不同的工作方式,但这与上面所述的JavaScript代码无关。

#2


0  

Backreferences, cannot be used inside a character class. The \1 in a regex like (a)[\1b] is either an error or a needlessly escaped literal 1. In JavaScript it's an octal escape.

反向引用不能在字符类中使用。像(a)[\1b]这样的regex中的\1不是错误就是不必要的转义文字1。在JavaScript中,它是一个八进制的转义。

It's better explained at Using Regular Expressions with JavaScript - regular-expressions.info

更好的解释是使用正则表达式和JavaScript正则表达式。info


will work and in some

In some \1 will work and in some $1 in online regex builder.

在一些\1将工作,在一些$1的在线regex生成器。

Does both way works in every javascript engine?

这两种方法在每个javascript引擎中都有效吗?

2 个解决方案

#1


3  

Only one way works in JavaScript, and it differs if the back-reference is in the regular expression (\n) or the replacement value ($n).

只有一种方法可以在JavaScript中工作,如果反向引用在正则表达式(\n)或替换值($n)中,则会有所不同。

See MDN Regular Expressions:

看到MDN正则表达式:

[In a regular expression] where n is a positive integer, [\n refers to] a back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses).

[在正则表达式中]当n是一个正整数时,[\n指]一个与正则表达式中n个括号相匹配的最后子字符串的后置引用(计数左括号)。

For example, /apple(,)\sorange\1/ matches 'apple, orange,' in "apple, orange, cherry, peach."

例如,/apple()\sorange\1/匹配“apple, orange”中的“apple, orange, cherry, peach”。

..

. .

The \1 and \2 in the pattern match the string's last two words. Note that \1, \2, \n are used in the matching part of the regex. In the replacement part of a regex the syntax $1, $2, $n must be used, e.g.: 'bar foo'.replace( /(...) (...)/, '$2 $1').

模式中的\1和\2匹配字符串的最后两个单词。注意,regex的匹配部分使用了\1、\2、\n。在regex的替换部分,语法必须使用$1、$2、$n,例如:“bar foo”。替换(/(…)(…)/,“$2 $1”)。

That way is the correct way in JavaScript; different regular expressions may work differently, but such is irrelevant to JavaScript code which works as stated above.

这种方法在JavaScript中是正确的;不同的正则表达式可能有不同的工作方式,但这与上面所述的JavaScript代码无关。

#2


0  

Backreferences, cannot be used inside a character class. The \1 in a regex like (a)[\1b] is either an error or a needlessly escaped literal 1. In JavaScript it's an octal escape.

反向引用不能在字符类中使用。像(a)[\1b]这样的regex中的\1不是错误就是不必要的转义文字1。在JavaScript中,它是一个八进制的转义。

It's better explained at Using Regular Expressions with JavaScript - regular-expressions.info

更好的解释是使用正则表达式和JavaScript正则表达式。info


in online regex builder. in online r




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: