我有这个JS代码:
var str = "foo bar";
var res1 = str.replace(new RegExp('foo\\b', 'g'), "BAZ");
var res2 = str.replace(new RegExp('foo', 'g'), "BAZ");
console.log("Result1: " + res1 + " Result2: " + res2);
在Chrome Version 69.0.3497.81 (Official Build) (64-bit)
控制台上的结果是:
Result1: BAZ bar Result2: BAZ bar
现在我在PHP
上以扩展名V8Js
测试相同的代码:
PHP代码:
<?php
$v8 = new V8Js();
$JS = <<<EOT
var str = "foo bar";
var res1 = str.replace(new RegExp('foo\\b', 'g'), "BAZ");
var res2 = str.replace(new RegExp('foo', 'g'), "BAZ");
print("Result1: " + res1 + " Result2: " + res2);
EOT;
echo $v8->executeString($JS);
扩展为PHP 7.2.9 (cli) (built: Aug 15 2018 05:57:41) ( NTS MSVC15 (Visual C++ 2017) x64 )
的{{1}}上的结果:
V8Js Version 2.1.0
为什么Result1: foo bar Result2: BAZ bar
的结果不同?!!!
答案 0 :(得分:2)
您使用的Heredoc与{"CreatePassengerNameRecordRS":{"ApplicationResults":{"status":"NotProcessed","Error":[{"type":"BusinessLogic","timeStamp":"2018-09-11T08:45:51.946-05:00","SystemSpecificResults":[{"Message":[{"code":"ERR.SWS.HOST.ERROR_IN_RESPONSE","content":"Unable to perform air booking step"}]}]}],"Warning":[{"type":"Application","timeStamp":"2018-09-11T08:45:51.946-05:00","SystemSpecificResults":[{"Message":[{"code":"WARN.SP.PROVIDER_ERROR","content":"EnhancedAirBookRQ: CODE - UC SEG STATUS NOT ALLOWED-0003"}]}]}]}},"Links":[{"rel":"self","href":"https://api.test.sabre.com/v2.1.0/passenger/records?mode=create"},{"rel":"linkTemplate","href":"https://api.test.sabre.com//passenger/records?mode="}]}
等效。
这意味着它将把"
解释为转义。
如果使用Nowdoc,它将与\
等效,因此不能逃脱反斜杠。
当阅读手册时,它并不完全,但是您需要阅读Nowdoc才能知道Heredoc是双引号。
现在文档将单引号引起来的字符串,此处文档将双引号引起来的字符串。
这意味着将您的字符串声明更改为:
'