[英]Replace regExp with certain characters (Javascript)
I have having some issues trying to replace a certain group of special characters in a string. 我在尝试替换字符串中的某些特殊字符组时遇到一些问题。 Specifically
特别
str = 'This is some demo copy[300]this should remove the brackets and the copy 300.'
I have been trying to replace it with 我一直在尝试用
str.replace(/[300]/g, "<br />");
with no such luck. 没有这种运气。 I seem to be getting hung on with the brackets
[]
that also need to be removed. 我似乎对必须删除的方括号
[]
感到困惑。 I know that I can remove them individually, and then replace the 300 but I need to replace the set together. 我知道我可以单独删除它们,然后替换300,但是我需要一起替换。
It should return: 它应该返回:
This is some demo copy
this should remove the brackets and the copy 300.
Any advice would be extremely appreciative as I am relatively new to regular expressions. 任何建议都是非常感激的,因为我是正则表达式的新手。
Thanks in advance! 提前致谢!
In regular expressions, [
and ]
have special meaning. 在正则表达式中,
[
和]
具有特殊含义。 You're asking to replace any 3
or 0
that might be found. 您要求替换可能找到的任何
3
或0
。
Escape the brackets like so: 像这样逃避括号:
str.replace(/\[300\]/g, "<br />");
Here is a regex for you: 这是给您的正则表达式:
/\[300\]/g
Brackets in regular expressions language define a character set. 正则表达式语言中的方括号定义字符集。 In your case you simply need to escape them to make things working.
在您的情况下,您只需要使它们脱离运行即可。
您需要转义方括号。
str.replace(/\[300\]/g, "<br />");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.