【问题标题】:Javascript regex handle /Javascript 正则表达式句柄 /
【发布时间】:2013-12-14 19:24:41
【问题描述】:

我需要从字面上替换这个字符串:/:)

但是,如果我这样做

test = text.replace(//:\)/gi, replacement);

Javascript 会将// 视为注释的开头。如果我这样做(添加括号):

test = text.replace(/(/:\))/gi, replacement);

这是一个语法错误,因为它将/(/ 视为模式

我能做些什么来解决这个问题?

【问题讨论】:

    标签: javascript regex


    【解决方案1】:

    用反斜杠转义正斜杠怎么样:

    test = text.replace(/\/:\)/gi, replacement);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 2021-06-08
    • 2012-11-23
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多