“\ 3”在正则表达式中的含义是什么?

时间:2017-09-06 14:49:44

标签: javascript regex

我知道javascript“(x)”可以记住模式和匹配,但我有一个问题。 这是我的代码:

 var re = /(\w*)(abc)\1\2/;
 var str = '123dabc123dabc'
 console.log(re.exec(str))

 var re = /(\w*)(abc)\1\3/;
 var str = '123dabc123dabc'
 console.log(re.exec(str)) // this must be null,because "\3" is not the right pattern,but what·s the "\3" ·s match rules in here

我想知道哪个str \3可以匹配?...

1 个答案:

答案 0 :(得分:4)

当引用不存在的捕获组索引时,它引用字符索引。

所以\3匹配字符??,索引为3 ^ 8(3 ^ 10或3 ^ 16)

??因为没有字符映射到此。如果您写了另一个八进制值,比如说\51,那么您将匹配)。请注意,如果您(想)使用\48作为八进制值,这将无效,它将被视为\4,后跟文字8

它非常类似于使用十六进制时,您可能已经看过\ x30例如......

\x30匹配字符0,索引为30 ^ 16(48 ^ 10或60 ^ 8)

相关问题