【问题标题】:Replace a special character in a regular expression using that /g [duplicate]使用 /g 替换正则表达式中的特殊字符 [重复]
【发布时间】:2016-06-22 09:13:02
【问题描述】:

我从一个查询参数中得到,一个包含转义空格的字符串,看起来像这样“I+have+to+go”。

我正在使用替换功能来做到这一点。

'I+have+to+go'.replace('+',' ') // Gives I have+to+go
'I+have+to+go'.replace(/+/g, '') // Doesn't work ! Probably because + is a 

JavaScript 正则表达式的特殊字符。

那么有没有办法在正则表达式中转义+。

【问题讨论】:

    标签: javascript regex query-string


    【解决方案1】:

    这行得通。

    console.log('I+have+to+go'.replace(/\+/g, ' '));
    

    【讨论】:

      猜你喜欢
      • 2018-01-14
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      相关资源
      最近更新 更多