Escaping, Special Characters
On this page
Escaping, Special Characters (逃脱,特殊字符)
The escape function is a global object property. It figures a new string where particular characters are replaced by a hexadecimal escape sequence. (Escape函数是一个全局对象属性。它表示一个新字符串,其中特定字符被十六进制转义序列替换。)
The special characters are usually encoded by using the exception of @*_+-./. They are used mainly for doing more powerful searches. The full list of the special characters looks as follows: [ \ ^ $ . | ? * + ( ).
Escaping
Escaping (逃亡)
Imagine that you want to search for a dot. To meet that goal you need to use a special character like a regular one, prepending it with a backslash \ (also known as “escaping a character”), like this:
console.log("Chapter 7.1".match(/\d\.\d/)); // 7.1 (match!)
console.log("Chapter 711".match(/\d\.\d/)); // null (searching for a real dot \.)
Parentheses also relate to the special characters. Hence, if you want them you can use (. Let’s try to look for “g()” string:
console.log("function g()".match(/g\(\)/)); // "g()"
If you are searching for a backslash , it is a special character in both regexps and strings. So, it should be doubled like this:
console.log("1\\2".match(/\\/)); // '\'
A Slash
A Slash (一个斜杠)
Although a slash ‘/’ is not a special character, in JavaScript, it is used for opening and closing a regexp. Therefore, you should escape it, too. (虽然斜杠“/”不是特殊字符,但在JavaScript中,它用于打开和关闭正则表达式。因此,您也应该逃离它。)
The search for a ‘/’ is visualized in the example below:
console.log("/".match(/\//)); // '/'
In case you don’t use /…/ but create regexp using new RegExp, then it is not necessary to escape it:
console.log("/".match(new RegExp("/"))); // finds /
The New Regexp
While creating a regular expression using new RegExp, then it is not necessary to escape / but some other escaping should be done. (在使用新的RegExp创建正则表达式时,无需转义/,但应执行其他一些转义。)
Let’s consider the following example:
let regexp = new RegExp("\d\.\d");
console.log("Chapter 3.2".match(regexp)); // null
Such a search works with /\d.\d/, but new RegExp("\d.\d") is invalid. Let’s find out why. (此类搜索适用于/\ d.\ d/,但new RegExp ("\ d.\ d")无效。让我们找出原因。)
The primary reason is that a string consumes backslashes. Regular strings contain own special characters such as \n. And backslash is applied for escaping. (主要原因是字符串使用反斜杠。常规字符串包含自己的特殊字符,例如\ n。反斜杠用于逃逸。)
The“\d.\d” is perceived in this way:
console.log("\d\.\d"); // d.d
Backslashes are consumed by string quotes and interpreted on their own, as follows:
\n transforms into a newline character. (-\ n转换为换行符。)
\u1234 transforms into the Unicode character. (-\ u1234转换为Unicode字符。)
When there is not any special meaning such as \d or \z, the backslash should be removed. (-当没有任何特殊含义(如\ d或\ z )时,应删除反斜杠。)
The new RegExp receives a string without the backslash. For this reason, the search fails. (新的RegExp接收一个没有反斜杠的字符串。因此,搜索失败。)
For fixing it, you should double backslashes as string quotes turn \ into , like here:
let regStr = "\\d\\.\\d";
console.log(regStr); // \d\.\d (correct now)
let regexp = new RegExp(regStr);
console.log("Chapter 3.2".match(regexp)); // 3.2
Summary
Summary (概要)
Each programming language has its special characters, meaning something special such as identifying a variable, the end of a line, and so on. JavaScript also provides a range of functions that can encode and decode special characters. (每种编程语言都有其特殊字符,这意味着一些特殊的东西,如标识变量、行尾等。JavaScript还提供了一系列可以编码和解码特殊字符的函数。)
So, the escape function, in JavaScript is considered a property of the global object. Special characters are used for making more powerful and significant searches. (因此,在JavaScript中,转义函数被视为全局对象的属性。特殊字符用于进行更强大、更重要的搜索。)