【发布时间】:2020-03-01 14:03:42
【问题描述】:
Javascript 代码:
var testStr = "this is inside the {replace} expression";
var test2Str = "second $ short";
var test3Str = "third '$' short";
var test4Str = "third '$ ' short";
var test5Str = "third $' short";
var test6Str = "third '$$' short";
var test7Str = testStr.replace('{replace}', test2Str);
var test8Str = testStr.replace('{replace}', test3Str);
var test9Str = testStr.replace('{replace}', test4Str);
var test10Str = testStr.replace('{replace}', test5Str);
var test11Str = testStr.replace('{replace}', test6Str);
console.log("test2Str: " + test2Str);
console.log("test3Str: " + test3Str);
console.log("test4Str: " + test4Str);
console.log("test5Str: " + test5Str);
console.log("test6Str: " + test6Str);
console.log("test7Str: " + test7Str);
console.log("test8Str: " + test8Str);
console.log("test9Str: " + test9Str);
console.log("test10Str: " + test10Str);
console.log("test11Str: " + test11Str);
产生:
test2Str: second $ short
test3Str: third '$' short
test4Str: third '$ ' short
test5Str: third $' short
test6Str: third '$$' short
test7Str: this is inside the second $ short expression
test8Str: this is inside the third ' expression short expression
test9Str: this is inside the third '$ ' short expression
test10Str: this is inside the third expression short expression
test11Str: this is inside the third '$' short expression
由于某种原因,'$ 导致替换函数将替换字符串分成两部分:一部分在正确的位置被替换,另一部分在整个字符串的末尾。这种行为在 Chrome 和 node v10.16.2 命令行中是相同的。有任何想法吗?是否还有其他可能导致类似问题的转义字符?
【问题讨论】:
-
欢迎来到 Stack Overflow!请环顾四周,并通读help center,尤其是How do I ask a good question?,我还推荐Jon Skeet 的Writing the Perfect Question。发帖前请search。更多关于搜索here。
标签: javascript node.js google-chrome