如何从javascript var中删除反斜杠?

编程入门 行业动态 更新时间:2024-10-15 02:25:47
本文介绍了如何从javascript var中删除反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个var

var x = "<div class=\\\"abcdef\\\">";

哪个是

<div class=\"abcdef\">

但是我需要

<div class="abcdef">

我如何unes​​cape这个var可以删除所有转义的字符?

How can I "unescape" this var to remove all escaping characters?

推荐答案

您可以通过正则表达式替换一个反斜杠后跟引号,而 String#replace function:

You can replace a backslash followed by a quote with just a quote via a regular expression and the String#replace function:

var x = "<div class=\\\"abcdef\\\">"; x = x.replace(/\\"/g, '"'); document.body.appendChild( document.createTextNode("After: " + x) );

请注意正则表达式只是一个反斜杠在文字中有两个,因为您必须使用反斜杠在正则表达式文字中转义反斜杠(就像字符串文字中一样)。

Note that the regex just looks for one backslash; there are two in the literal because you have to escape backslashes in regular expression literals with a backslash (just like in a string literal).

在正则表达式的末尾,g 告诉替换在整个字符串(全局)中工作;否则,它只会替换第一场比赛。

The g at the end of the regex tells replace to work throughout the string ("global"); otherwise, it would replace only the first match.

更多推荐

如何从javascript var中删除反斜杠?

本文发布于:2023-11-06 08:11:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563155.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斜杠   javascript   var

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!