为了账号安全,请及时绑定邮箱和手机立即绑定

如果替换具有$',则replace方法会产生意外结果

如果替换具有$',则replace方法会产生意外结果

不负相思意 2021-05-16 12:13:09
当替换字符串包含$'个字符时,请用奇怪的字符串替换方法结果。示例: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?
查看完整描述

2 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

你应该逃避角色

replace(regex, "\$")

替换目标中的$具有特殊含义:将匹配组放入字符串中


查看完整回答
反对 回复 2021-05-27
  • 2 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信