gpt4 book ai didi

javascript - 当你转义数字时会发生什么?

转载 作者:行者123 更新时间:2023-11-28 05:24:46 25 4
gpt4 key购买 nike

我有这段代码,用反向引用替换了REGEX捕获组的匹配,但匹配的文本总是被替换为任何内容,即被剪切。

代码:

$str = 'string is string';
$replacement_result = preg_replace("/(\w+) is (\w+)/","\1 this \3", $str);

echo $replacement_result;
// results was 'is'

然后我注意到替换参数字符串在双引号中并且正在转义数字(我认为这可以正常工作),因此我将双引号更改为单引号

$str = 'string is string';
// changes here
$replacement_result = preg_replace("/(\w+) is (\w+)/",'\1 this \3', $str);

echo $replacement_result;
// results was 'string this string'

我什至在 Javascript 中尝试了 document.write() 和转义数字,结果是相同的(什么也没有)。

显然这是我第一次遇到这个问题,并且有一个非常简单的 1 - 2 行答案。当数字被转义时会发生什么?

最佳答案

根据official documentation ,在双引号字符串中,一系列反斜杠后跟 0..7 范围内的 1..3 数字被解释为八进制表示法中的字符:

\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000")

关于javascript - 当你转义数字时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40261488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com