Javascript字符串中的单一和双重转义(反斜杠)有什么区别? [英] What is the difference between a single and double escape (backslash) in Javascript string?

查看:288
本文介绍了Javascript字符串中的单一和双重转义(反斜杠)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试回答这个帖子的问题



另​​一个SO帖子



我注意到这个

 函数('return'\\101\\40\\171\\145\\154\\154\\157\\ \\\167\\40\\142\\165\\164\\164\\157\\156\\56')()

 函数('return'\101\40\171\145\154\154\157\167\40\142\165\164\164\157 \156\56')()

都返回相同的字符串 - 黄色按钮。



关于我链接到上面的问题,JS代码片段在标签中运行。



我也曾经注意到,在处理某些JSON字符串时,'\'需要超过'\'



什么时候需要?
为什么有必要?
如果没有必要,那么为什么会这样做?



链接和附加阅读对我有帮助。



更新:
也许我没有提供足够的信息这个问题。



我明白为什么逃避需要。



我想知道的是两件事:

在上面的链接中,代码有两个功能;一个嵌套在另一个像这样

 函数(
函数(
'return\'\ \141\\75\\160\\162\\157\\155\\160\\164\\50\\47\ \105\\156\\164\\162\\145\\172\\40\\154\\145\\40\ \155\\157\\164\\40\\144\\145\\40\\160\\141\\163\ \163\\145\\47\\51\\73\\151\\146\\50\\141\\75\ \75\\47\\164\\157\\164\\157\\61\\62\\63\\154\ \157\\154\\47\\51\\173\\141\\154\\145\\162\\164\ \50\\47\\142\\162\\141\\166\\157\\47\\51\\73\ \175\\145\\154\\163\\145\\173\\141\\154\\145\\162\ \164\\50\\47\\146\\141\\151\\154\\56\\56\\56\ \47\\51\\73\\175\\\73'\\\175 \'')())()

此脚本与此脚本一样工作

 函数(
函数('return' \141\75\160\162\157\155\160\164\50\47\105\156\164\162\145\172\ 40\154\145\40\155\157\164\40\144\145\40\160\141\163\163\145\47\\ \\51\73\151\146\50\141\75\\ \\75\47\164\157\164\157\61\62\63\154\157\154\47\51\173\141\154 \145\162\164\50\47\142\162\141\166\157\47\51\73\175\145\154\ 163\145\173\141\154\145\162\164\50\47\146\141\151\154\56\56\56\\ \\ 47\51\73\175')())()

如果两个脚本都能工作,那么\需要\\\?有没有我在这里失踪的东西?



第二个问题是,



当 \\将需要超过\?



我认为sventschui正在到那里,但还没有。

解决方案

我认为 sventschui 有正确的想法,让我尝试以不同的方式解释它。



当您使用
定义一个字符串时,str1 ='\\101\\ \\\40\\171\\145\\154\\154\\157\\167\\40\\142\\165\\ \\\164\\164\\157\\156\\56'



code> \\ 是转义序列,内存中的实际字符串(包括它周围的引号)\101\40\171 \145\154\154\157\167\ 40\142\165\164\164\157\156\56






当您将字符串定义为 str2 =\101\40\171\145\154\154\157\167\40\ 142 \165\164\164\157\156\56



\\ \\ 101 \40 ,...是转义序列,内存中的实际字符串(包括其周围的引号)黄色按钮






创建 Function(),它重新评估字符串(如 eval ),对于第一种情况, str1 现在它将 \101 \40 作为转义序列,并返回字符串是一个黄色按钮,没有引号。



当你对第二个字符串做同样的事情,没有转义序列,只是普通字符,所以字符串不变(q除外)在它周围发表)



  var str1 ='\\101 ; //\101var str2 ='\101'; //Avar str1Evaled = eval(str1); // \101是转义序列,输出Avar str2Evaled = eval(str2); //没有转义序列,一个原始的Aconsole.log({str1:str1,str2:str2,str1Evaled:str1Evaled,str2Evaled:str2Evaled}); // Object {str1:\101,str2:A ,str1Evaled:A,str2Evaled:A}  


After attempting to answer the question to this post

Another SO post

I noted that this

Function('return "\\101\\40\\171\\145\\154\\154\\157\\167\\40\\142\\165\\164\\164\\157\\156\\56"')()

or

Function('return "\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"')()

both return the same string - "A yellow button."

In regards to the question I linked to above, the JS code snippet is run in a tag.

I have also noted in the past that when working on some JSON strings, that the '\' was needed over '\'

When is it necessary? Why is it necessary? If it is not necessary, then why is it done?

Links and additional reading are helpful to me.

UPDATE: Perhaps I did not ask this question with enough information.

I understand when and why escaping is needed.

What I would like to know are two things

In the link above, the code has two function; one nested in the other like so

Function(
  Function(
    'return\'\\141\\75\\160\\162\\157\\155\\160\\164\\50\\47\\105\\156\\164\\162\\145\\172\\40\\154\\145\\40\\155\\157\\164\\40\\144\\145\\40\\160\\141\\163\\163\\145\\47\\51\\73\\151\\146\\50\\141\\75\\75\\47\\164\\157\\164\\157\\61\\62\\63\\154\\157\\154\\47\\51\\173\\141\\154\\145\\162\\164\\50\\47\\142\\162\\141\\166\\157\\47\\51\\73\\175\\145\\154\\163\\145\\173\\141\\154\\145\\162\\164\\50\\47\\146\\141\\151\\154\\56\\56\\56\\47\\51\\73\\175\'')())()

This script works as well as this script

Function(
  Function('return "\141\75\160\162\157\155\160\164\50\47\105\156\164\162\145\172\40\154\145\40\155\157\164\40\144\145\40\160\141\163\163\145\47\51\73\151\146\50\141\75\75\47\164\157\164\157\61\62\63\154\157\154\47\51\173\141\154\145\162\164\50\47\142\162\141\166\157\47\51\73\175\145\154\163\145\173\141\154\145\162\164\50\47\146\141\151\154\56\56\56\47\51\73\175"')())()

Why, if both scripts work would the "\\" be needed over "\"? Is there something I am missing here?

The second question would be,

Are there fundamental occurances when "\\" would be required over "\"?

I think sventschui is getting there, but not quite yet.

解决方案

I think sventschui has the right idea, let me try to explain it in a different way.

When you define a string using str1='"\\101\\40\\171\\145\\154\\154\\157\\167\\40\\142\\165\\164\\164\\157\\156\\56"'

The \\ is the escape sequence and the actual string in memory is (including the quotes around it) "\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"


When you defined a string as str2="\101\40\171\145\154\154\157\167\40\142\165\164\164\157\156\56"

The \101,\40,... are the escape sequences and the actual string in memory is (including the quotes around it)"A yellow button"


When you create a Function(), it re-evaluates the strings (like eval), for the first case, str1 now it treats the \101, \40 as escape sequences, and the returned string is A yellow button, without the quotes around it.

When you do the same thing to the second string, there are no escape sequences, just regular characters, so the string is unchanged (except for the quotes around it)

var str1 = '"\\101"'; // "\101"
var str2 = '"\101"'; //  "A"

var str1Evaled = eval(str1); // \101 is the escape sequence, outputs A
var str2Evaled = eval(str2); // No escape sequence, a raw A

console.log({str1: str1, str2: str2, str1Evaled: str1Evaled, str2Evaled:str2Evaled});

//  Object {str1: ""\101"", str2: ""A"", str1Evaled: "A", str2Evaled: "A"}

这篇关于Javascript字符串中的单一和双重转义(反斜杠)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆