javascript 避免在javascript中转义特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17551867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:44:46  来源:igfitidea点击:

avoid to escape a special characters in javascript

javascript

提问by raky

My server returns value as support\testing. When I get this value in client it can be escaped as support testing. \tis escaped as tab space.

我的服务器返回值为support\testing. 当我在客户端获得这个值时,它可以转义为support testing. \t被转义为制表符空间。

How do I avoid escaping special characters in JavaScript?

如何避免在 JavaScript 中转义特殊字符?

回答by Spudley

Your server needs to output the string with proper escaping.

您的服务器需要以正确的转义方式输出字符串。

In this case, you want a backslash character in the output; backslash is a special character, so that should be escaped.

在这种情况下,您需要在输出中使用反斜杠字符;反斜杠是一个特殊字符,因此应该对其进行转义。

The escape sequence for a backslash is \\(ie two backslashes), but you shouldn't need to think about specific escape codes -- if you're outputting JS data, you should be outputting it using proper escaping for the whole string, which generally means you should be using JSON encoding.

反斜杠的转义序列是\\(即两个反斜杠),但您不需要考虑特定的转义码——如果您正在输出 JS 数据,您应该使用整个字符串的正确转义来输出它,通常意味着您应该使用 JSON 编码。

Most server languages these days provide JSON encoding as a built-in feature. You haven't specified which language your server is using, but for example if it's written in PHP, you would output your string as json_encode($string)rather than just outputting $stringdirectly. Other languages provide a similar feature. This will protect you not just from broken backslash characters, but also from other errors, such as quote marks or line feeds in your strings, which will also cause errors if you put them into a Javascript code as an unescaped string.

如今,大多数服务器语言都提供 JSON 编码作为内置功能。您还没有指定您的服务器使用哪种语言,但例如,如果它是用 PHP 编写的,那么您将输出字符串json_encode($string)而不是直接输出$string。其他语言提供了类似的功能。这不仅可以保护您免受损坏的反斜杠字符,还可以防止其他错误,例如字符串中的引号或换行符,如果您将它们作为未转义的字符串放入 Javascript 代码中,也会导致错误。

回答by Alexander Praetorius

You can use tagged template literals

您可以使用标记的模板文字

var str = (s => s.raw)`support\testing`[0]

The anonymous arrow function will serve as tag and s.rawcontains the original input

匿名箭头函数将作为标签并s.raw包含原始输入

回答by marteljn

If you are able to change the server-side code, you should add the escape character there: "support\\testing".

如果您能够更改服务器端代码,则应在此处添加转义字符:"support\\testing"

That will result in the desired result.

这将导致预期的结果。

回答by jh314

You can do a simple replace:

你可以做一个简单的替换:

str.replace("\t","\t");

And do this for other characters you need replacing.

并为您需要替换的其他字符执行此操作。

回答by zaid

Best Solution for this

最佳解决方案

function valid(f) {            
        debugger;
        var s = "!@#$%^&*()+=-[]\\';,./{}|\":<>?~";
        str = f.value;
        for (var i = 0; i < str.length; i++) {
           if (s.indexOf(str.charAt(i)) != -1) {
              //alert("The box has special characters. \nThese are not allowed.\n");
              f.value = f.value.replace(str.charAt(i), '');// : null;
              return false;
            }
        }
 }