作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当替换字符串有$'字符时,将方法结果替换为奇怪的字符串。
示例:https://jsfiddle.net/1jns2eo9/
var p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
var regex = /dog/gi;
document.getElementById('first').innerHTML = p.replace(regex, '$');
/// The quick brown fox jumps over the lazy $. If the $ reacted, was it really lazy?
document.getElementById('second').innerHTML = p.replace(regex, "$'");
/// The quick brown fox jumps over the lazy . If the dog reacted, was it really lazy?. If the reacted, was it really lazy? reacted, was it really lazy?
最佳答案
你应该使用
来转义这个字符replace(regex, "\$")
替换目标中的$有特殊含义:将匹配组放入字符串
关于javascript - 如果替换有 $',replace 方法会产生意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077596/
我是一名优秀的程序员,十分优秀!