如何转义字符串中的字符

时间:2022-11-20 22:27:44

Consider below response of ajax-

考虑下面ajax的响应 -

function validateVar(){
    $.ajax({
        url:"abc.do",
        datatype:"text",
        success:function(response){
            alert(response);    //"asdjakd"fsd'f'fsf"s'dfs'df"fsdf"fsfsf""
        }
    });
}

I want to escape all occurrences of quotes in the response.

我想逃避响应中出现的所有引号。

JSFIDDLE: https://jsfiddle.net/ashwyn/3w0aj5z7/

5 个解决方案

#1


0  

You can use the backslash to escape a character \. So to store your string in a variable we can write it like this.

您可以使用反斜杠来转义字符\。因此,要将字符串存储在变量中,我们可以像这样编写它。

var v = "asdjakd\"fsd\'f\'fsf\"s\'dfs\'df\"fsdf\"fsfsf";

#2


0  

You can't programmatically escape a string to make it valid JavaScript.

您无法以编程方式转义字符串以使其成为有效的JavaScript。

It has to be valid JavaScript to get through the JavaScript parser before it can be modified by JavaScript.

它必须是有效的JavaScript才能通过JavaScript解析器,然后才能被JavaScript修改。

You have to fix it in the source code.

你必须在源代码中修复它。

The \ character starts an escape sequence in a JavaScript string literal:

\ _字符在JavaScript字符串文字中启动转义序列:

var v = "asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf";

#3


0  

You can use backslash to solve this You need to add backslash for same type inverted commas(single or double).

您可以使用反斜杠来解决此问题您需要为相同类型的引号(单或双)添加反斜杠。

function validateVar(){
   var v = "asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf";
   alert(v);
}

Update the fiddle as well, check here :: https://jsfiddle.net/3w0aj5z7/1/

更新小提琴,请点击这里:: https://jsfiddle.net/3w0aj5z7/1/

#4


0  

You need to escape the double quotationmark " with backslash \

你需要转义双引号“用反斜杠\

Your string should look like this:

你的字符串应如下所示:

"asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf"

#5


0  

You can include the string between '' or "" . Then you must say to the variable where quotes aren't the closing ones with \ before it.

您可以在''或'“之间包含字符串。然后你必须对变量说,引号不是在它之前的\与结束的引号。

In this case:

在这种情况下:

var v = ' "asdjakd"fsd\'f\'fsf"s\'dfs\'df"fsdf"fsfsf" ';

before ' quote

在'报价之前

or

var v = " \"asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf\" ";

before " quotes

之前“引用

#1


0  

You can use the backslash to escape a character \. So to store your string in a variable we can write it like this.

您可以使用反斜杠来转义字符\。因此,要将字符串存储在变量中,我们可以像这样编写它。

var v = "asdjakd\"fsd\'f\'fsf\"s\'dfs\'df\"fsdf\"fsfsf";

#2


0  

You can't programmatically escape a string to make it valid JavaScript.

您无法以编程方式转义字符串以使其成为有效的JavaScript。

It has to be valid JavaScript to get through the JavaScript parser before it can be modified by JavaScript.

它必须是有效的JavaScript才能通过JavaScript解析器,然后才能被JavaScript修改。

You have to fix it in the source code.

你必须在源代码中修复它。

The \ character starts an escape sequence in a JavaScript string literal:

\ _字符在JavaScript字符串文字中启动转义序列:

var v = "asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf";

#3


0  

You can use backslash to solve this You need to add backslash for same type inverted commas(single or double).

您可以使用反斜杠来解决此问题您需要为相同类型的引号(单或双)添加反斜杠。

function validateVar(){
   var v = "asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf";
   alert(v);
}

Update the fiddle as well, check here :: https://jsfiddle.net/3w0aj5z7/1/

更新小提琴,请点击这里:: https://jsfiddle.net/3w0aj5z7/1/

#4


0  

You need to escape the double quotationmark " with backslash \

你需要转义双引号“用反斜杠\

Your string should look like this:

你的字符串应如下所示:

"asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf"

#5


0  

You can include the string between '' or "" . Then you must say to the variable where quotes aren't the closing ones with \ before it.

您可以在''或'“之间包含字符串。然后你必须对变量说,引号不是在它之前的\与结束的引号。

In this case:

在这种情况下:

var v = ' "asdjakd"fsd\'f\'fsf"s\'dfs\'df"fsdf"fsfsf" ';

before ' quote

在'报价之前

or

var v = " \"asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf\" ";

before " quotes

之前“引用