我在网上到处找答案,
为什么我的.replace只替换没有其他字符的字符
我的代码是:const fixedstring = string.replace('&', '^&')
当我输入:¬epad.exe&有一个字符串时,它给我输出^¬epad.exe&而不是^¬epad.exe^&
有人能帮我吗?(我试过/g了)
谢谢你能帮我或者只是读我的帖子,谢谢!
发布于 2022-07-03 18:37:30
除非使用regex查找要替换的字符,否则不能使用全局标志/g。您目前正在使用一个字符串来查找,这将只匹配第一个实例。
相反,使用regex:
.replace(/&/g, "^&")下面是一个有用的例子。
const stringToReplace = "¬epad.exe&"
const replaced = stringToReplace.replace(/&/g, '^&')
console.log(replaced)
https://stackoverflow.com/questions/72848956
复制相似问题