gpt4 book ai didi

javascript - 更换|| (连接)在字符串中

转载 作者:行者123 更新时间:2023-11-28 13:54:44 29 4
gpt4 key购买 nike

我在替换 || 时遇到问题字符。

    str="Example || sentence";

document.write(str.replace(/||/g, "+"));
// it gives me "+ +E+x+a+m+p+l+e+ +|+|+ +s+e+n+t+e+n+c+e+"

如何修复它?

最佳答案

| 是一个正则表达式运算符,其行为类似于 or。如果你想在字符串中匹配它,你需要转义它:

str = "Example || sentence";
document.write(str.replace(/\|\|/g, "+"));

关于javascript - 更换|| (连接)在字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8668991/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com