A special character in a re

Special characters are characters that have special meanings or functions in regular expressions. They cannot be interpreted as literal characters. For example, * means zero or more occurrences. Regular expression in the common special characters are [\ ^ $|? * + () (focus here is not a special character, more special characters and its usage examples can view the Regular expression syntax cheatsheet).

Note that the slash is not a special character.

escape

Is a way to turn a special character into a normal character by prefixing it with \, also known as an escape character.

For example, if you want to match a. In a string with a regular expression, but. Is a special character in a regular expression. By default, it means to match any single character except a newline, so you can use \. Decimal number into a literal meaning, such as matching a regular expression/decimal ^ ([0-9] {1} \. [0-9] *) $/.

// javascript
var myRe = / ^ ([0-9] {1} \. [0-9] *) $/;
/** * digression: * - Due to point (.) Special characters such as and asterisks (*) have no special meaning in the same character set (enclosed by brackets "[]"). * therefore, it is possible to write /^([0-9]{1,}[.][0-9]*)$/ without escaping * - but escaping also works. /^([0-9]{1,}[\.][0-9]*)$/ has the same effect */
myRe.test("42.0"); // true
myRe.test("42");   // false
Copy the code

If a special character is preceded by the escape character \, it indicates that the character is a common character instead of a special character. What happens if you prefix \ before a non-special character?

A backslash before a non-special character indicates that the next character is a special character and cannot be interpreted literally (emMM). For example, in a regular expression \b means to match the boundary of a word, for example

var myRe = /\bm/;
'moon'.match(myRe); // ["m", index: 0, input: "moon", groups: undefined]
Copy the code

The/character is not a special character, but in Javascript, it starts and ends regular expression literals: /… pattern… /. So if you want to match the slash itself, you also need to escape.

"/".match(/ / / /); // ["/", index: 0, input: "/", groups: undefined]

Under the / * * / of this kind of writing is unable to run normally
"/".match(/ / /)
Copy the code

In addition, since \ is a special character, to match the meaning of the character \, we need to escape:

'\ \'.match(/ \ \ /); // ["\", index: 0, input: "\", groups: undefined]
// \ Is also an escape function in JavaScript strings, so you need to escape itself as well, as we'll see later
Copy the code

Special characters in JavaScript

In JavaScript, strings also have special characters, and the backslash \ is also an escape character in string literals.

var txt="We are the so-called "Vikings" from the north."
// Error: Uncaught SyntaxError: Unexpected identifier
Copy the code

In JavaScript, strings start or end with either single or double quotation marks. This means that the string above will be truncated to: We are the so-called.

To solve this problem, you must precede the quotation marks in “Viking” with a backslash (\). This converts each double quote to a literal character.

var txt="We are the so-called \"Vikings\" from the north."
console.log(txt); // "We are the so-called "Vikings" from the north."

/** * As an aside: * If only the quotation marks are displayed within a string, you can use single and double quotation marks to achieve this effect */
var txt='We are the so-called "Vikings" from the north.'
Copy the code

The backslash \ in a string also indicates an escape or a special character like \n that can only be used in a string. The reference “consumes” and interprets these characters, such as:

  • \nBecomes a newline character
  • \u1234Becomes a Unicode character containing the code point
  • Others have no particular meaning, like\dor\zIn this case, the backslash is removed

To get the \ character itself in a JavaScript string, use var STR = “\\”; Var STR = “\” returns an error. In fact, you need to “escape”.

Double escape

As stated above, \ is an escape character both in regular expressions and in JavaScript string literals.

There are two ways to create regular expressions in JavaScript:

Regular expression literals
var re = /ab+c/;

// Contains escape special characters
var myRe = /[a-z]:\\/i
Copy the code

Regular expression literals can be escaped by prefixing \ to special characters.

callRegExpObject constructor

The RegExp constructor takes a string as an argument that looks a lot like a regular expression literal, but you can’t directly replace the literal’s switch mark/with string quotes because, as stated above, “backslashes in string literals are also escape characters.”

var regStr = "\d\.\d";
console.log(regStr); // "d.d"
var regexp = new RegExp(regStr);
"Chapter 5.1".match(regexp); // null
Copy the code

As you can see from the above code, string quotes consume \, so when passing arguments to new RehExp, you need a double backslash \\, which is the meaning of double escape. One string escape, one regular expression escape.

Double escape of complex points:

var regexp1 = new RegExp("node_modules[/\\\\]");  // /node_modules[/\\]/
var regexp2 = new RegExp("[a-z]:\\\\"."i");   // /[a-z]:\\/i
Copy the code

As an aside, in ES5, there are two cases for arguments to the RegExp constructor.

  • The argument is a string, in which case the second argument represents the regular expression modifier (flag)

    var regex = new RegExp('xyz'.'i');
    / / equivalent to the
    var regex = /xyz/i;
    Copy the code
  • The argument is a regular representation, but you are not allowed to add modifiers with the second argument

    var regex = new RegExp(/xyz/i);
    / / equivalent to the
    var regex = /xyz/i;
    // The second argument cannot be used
    var regex = new RegExp(/xyz/.'i');
    // Uncaught TypeError: Cannot supply flags when constructing one RegExp from another
    Copy the code

ES6 changes this behavior. If the first argument to the RegExp constructor is a regular object, the second argument can be used to specify the modifier. Furthermore, the returned regular expression ignores the modifier of the original regular expression and uses only the newly specified modifier.

new RegExp(/abc/ig.'i')
// is equivalent to/ABC/I
Copy the code

reference

  • Useful. Javascript. The info/regexp – esca…
  • Developer.mozilla.org/zh-CN/docs/…
  • ECMAScript 6 Getting started – An extension to regular