【问题标题】:How to replace \" with " using regex?如何使用正则表达式将 \" 替换为 "?
【发布时间】:2021-07-08 03:34:33
【问题描述】:

我有一个返回 HTML 字符串的 API,但它在 API 中有一些额外的字符,我想用其他东西替换它。

<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0\">

我想用 " 替换 \" 使用 replace 这样它就变成了

<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0">

执行上述任务的正则表达式string.replace() 是什么?

【问题讨论】:

  • 上面第 2 行输入字符串的来源是什么?这些反斜杠看起来像是在它们后面的文字双引号中转义字符。也就是说,反斜杠并不真正“存在”。

标签: javascript regex replace str-replace


【解决方案1】:

你可以在没有 RegEx 的情况下简单地使用它

<?php
$a = "<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0\">";
$b = str_replace('\"', '"', $a);
var_dump($b);

【讨论】:

    【解决方案2】:

    在你的正则表达式中使用\\

    var text = '<meta name=\"viewport\" content=\"initial-scale=1.0, maximum-scale=1.0\">';
    
    var result  = text.replace(/\\"/g, '"');
    
    console.log(result);

    【讨论】:

      【解决方案3】:

      所以你必须用另一个 \ 来逃避 \

      yourstring.replace(/\\"/, '"')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-29
        • 1970-01-01
        • 2012-05-05
        • 2012-02-12
        • 1970-01-01
        相关资源
        最近更新 更多