gpt4 book ai didi

javascript - 用某些字符替换 regExp (Javascript)

转载 作者:行者123 更新时间:2023-11-30 10:16:17 25 4
gpt4 key购买 nike

我在尝试替换字符串中的某组特殊字符时遇到了一些问题。具体

str = 'This is some demo copy[300]this should remove the brackets and the copy 300.'

我一直在尝试用

代替它
str.replace(/[300]/g, "<br />");

没有这样的运气。我似乎对还需要删除的括号 [] 感到困惑。我知道我可以单独移除它们,然后更换 300,但我需要一起更换套装。

它应该返回:

This is some demo copy
this should remove the brackets and the copy 300.

任何建议都将不胜感激,因为我对正则表达式还比较陌生。

提前致谢!

最佳答案

在正则表达式中,[] 有特殊的含义。您要求替换可能找到的任何 30

像这样转义括号:

str.replace(/\[300\]/g, "<br />");

关于javascript - 用某些字符替换 regExp (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23522992/

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