dougu0824
2014-03-27 13:00
浏览 757
已采纳

如何从JavaScript中删除字符串中的反斜杠

I have a string like this:

<tr><td><span class=\'label label-info\'>Dialed</span></td><td>9804292145453</td><td>A Jana</td><td>0sec</td><td>6:18PM, Mar 24, 2014</td></tr>

I want to remove the backslash & want result like this :

<tr><td><span class='label label-info'>Dialed</span></td><td>9804292145453</td><td>A Jana</td><td>0sec</td><td>6:18PM, Mar 24, 2014</td></tr>

Please help.

图片转代码服务由CSDN问答提供 功能建议

我有一个这样的字符串:

&lt; tr&gt; ;&lt; td&gt;&lt; span class = \'label label-info \'&gt; Dialed&lt; / span&gt;&lt; / td&gt;&lt; td&gt; 9804292145453&lt; / td&gt;&lt; td&gt; A Jana&lt; / td&gt;&lt; ; td&gt; 0sec&lt; / td&gt;&lt; td&gt; 2014年3月24日下午6点18分&lt; / td&gt;&lt; / tr&gt;

我想删除反斜杠 &安培; 想要这样的结果:

&lt; tr&gt;&lt; td&gt;&lt; span class ='label label-info'&gt; Dialed&lt; / span&gt;&lt; / td&gt; &lt; td&gt; 9804292145453&lt; / td&gt;&lt; td&gt; A Jana&lt; / td&gt;&lt; td&gt; 0sec&lt; / td&gt;&lt; td&gt; 2014年3月24日下午6点18分&lt; / td&gt;&lt; / tr&gt; < / code>

请帮忙。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumaojin4008 2014-03-27 13:07
    已采纳

    You've said what you quoted above is a string, but it's unclear whether you mean this (shortened a bit):

    var str = "<tr><td><span class=\'label label-info\'>Dialed...";
    

    ...where what you've quoted is what you literally have within quotes, or this (note the backslashes):

    var str = "<tr><td><span class=\\'label label-info\\'>Dialed...";
    

    ...where what you've quoted is the actual content of the string, not part of a string literal.

    The first one above doesn't have any backslashes in it, it has escaped ' characters. The second one has backslashes in it.

    To remove the backslahes from the second one:

    str = str.replace(/\\/g, "");
    

    When you give a regular expression with the g flag to replace, it applies globally throughout the string. Backslashes have special meaning in regular expressions, and so I've had to escape the backslash (with another backslash, it's the escape character for regular expressions as well as strings). So in the above, I'm saying to replace all backslashes with an empty string.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题