In-depth analysis of Regular Expressions in js and Analysis of Regular Expressions in js

Source: Internet
Author: User
Tags string methods

In-depth analysis of Regular Expressions in js and Analysis of Regular Expressions in js

Reading directory

  • Create a regular expression
  • Special characters in Regular Expressions
  • \ (Backslash)
  • ^
  • $
  • *, +,. (Decimal point)
  • ? (Question mark)
  • (X)
  • (? : X)
  • X (? = Y), x (?! Y), x | y
  • {N}, {n, m }:
  • [Xyz], [^ xyz]
  • Others
  • Regular Expression flag
  • Use Regular Expressions

In many cases, the regular expression is often confused. Recently, I took the time to systematically learn the regular expression, as shown below:

Create a regular expression

Two methods are available: Direct Writing, consisting of the mode between the slashes, and calling the RegExp object constructor.

The code for creating two methods is as follows:

// Directly create const regex1 =/AB + c/; const regex2 =/^ [a-zA-Z] + [0-9] * \ W? _ $/Gi; // call const regex3 = new RegExp ('AB + C '); const regex4 = new RegExp (/^ [a-zA-Z] + [0-9] * \ W? _ $/, "Gi"); const regex5 = new RegExp ('^ [a-zA-Z] + [0-9] * \ W? _ $ ', 'Gi ');

It can be seen that when the RegExp constructor is called to create a regular expression, the first parameter can be a string or a directly created regular expression.

Note that the toLocaleString () and toString) () methods inherited by the RegExp instance both return the literal amount of the regular expression, regardless of the method in which the regular expression is created.

For example:

const ncname = '[a-zA-Z_][\\w\\-\\.]*';const qnameCapture = '((?:' + ncname + '\\:)?' + ncname + ')';const startTagOpen = new RegExp('^<' + qnameCapture);startTagOpen.toString();    // '/^<((?:[a-zA-Z_][\w\-\.]*\:)?[a-zA-Z_][\w\-\.]*)/' 

Special characters in Regular Expressions

\ (Backslash)

1. Add a backslash before a non-special character to indicate that the next character is special;

2. Translate the following special characters into words;

Note: \ translation is required when RegExp is used to construct a function, because \ is also a conversion character in the string.

^

1. Match the start of the input;

2. indicates the reverse character set when the first character in;

Example:

/^A/.exec('an A')    // null/^A/.exec('An E')    // ["A", index: 0, input: "An E"]

$

End of matching Input

/T $ /. exec ('eater ') // null/t $ /. exec ('eat') // ["t", index: 2, input: "eat"] *, + ,. (decimal point)

*: Matches the previous expression 0 or multiple times. Equivalent to {0 ,};

+: Match the previous expression once or multiple times. Equivalent to {1 ,};

.:

Match any single character except line breaks;

? (Question mark)

1. Match the previous expression 0 times or 1 time. Equivalent to {0, 1 };

2. if it is followed by any quantifiers * +? After {}, the quantifiers will be converted to non-Greedy (matching as few characters as possible), which is exactly the opposite of the greedy mode used by default;

3. Used for predicate

Example:

/\d+/.exec('123abc')       // ["123", index: 0, input: "123abc"]/\d+?/.exec('123abc')      // ["1", index: 0, input: "123abc"]

(X)

Match 'X' and keep matching items in mind. parentheses indicate capturing parentheses;

Example:

/(foo) (bar) \1 \2/.test('bar foo bar foo');  // false/(bar) (foo) \1 \2/.test('bar foo bar foo');  // true/(bar) (foo) \1 \2/.test('bar foo');      // false/(bar) (foo) \1 \2/.test('bar foo foo bar');  // false/(bar) (foo) \2 \1/.test('bar foo foo bar');  // true'bar foo bar foo'.replace( /(bar) (foo)/, '$2 $1' );  // "foo bar bar foo"

The '(foo)' and '(bar)' in mode/(foo) (bar) \ 1 \ 2/match and remember the first two words in the string "foo bar. In the mode, \ 1 and \ 2 match the last two words of the string.

Note: \ 1, \ 2, and \ n are used in the matching of regular expressions. In the replacement of regular expressions, use a syntax such as $1, $2, and $ n. For example, 'bar foo'. replace (/(...)/, '$2 $1 ').

(? : X)

Match 'X' but do not remember the match. This is called a non-capturing bracket;

Example:

'foo'.match(/foo{1,2}/)        // ["foo", index: 0, input: "foo"]'foo'.match(/(?:foo){1,2}/)      // ["foo", index: 0, input: "foo"]'foofoo'.match(/(?:foo){1,2}/)     // ["foofoo", index: 0, input: "foofoo"]'foofoo'.match(/foo{1,2}/)       // ["foo", index: 0, input: "foofoo"]

Use Case: Example expression /(? : Foo) {1, 2 }/. If the expression is/foo {1, 2}/, {1, 2}, it will only take effect for the last character 'o' of 'foo. If non-capturing parentheses are used, {1, 2} matches the entire 'foo' word.

X (? = Y), x (?! Y), x | y

X (? = Y): Match 'X' only when 'X' is followed by 'y ';

X (?! Y): Match 'X' only when 'X' is not followed by 'y ';

X | y: matching x or y

The two matching results do not contain y

Example:

'JackSprat'.match(/Jack(?=Sprat)/)      // ["Jack", index: 0, input: "JackSprat"]'JackWprat'.match(/Jack(?=Sprat)/)      // null'JackWprat'.match(/Jack(?=Sprat|Wprat)/)  // ["Jack", index: 0, input: "JackWprat"]/\d+(?!\.)/.exec("3.141")    // ["141", index: 2, input: "3.141"]

{N}, {n, m }:

{N}: match the previous character exactly happened n times;

{N, m}: match the previous character at least n times, up to m times. If the value of n or m is 0, this value is ignored;

Example:

  /a{2}/.exec('candy')     // null  /a{2}/.exec('caandy')    // ["aa", index: 1, input: "caandy"]  /a{2}/.exec('caaandy')    // ["aa", index: 1, input: "caaandy"]  /a{1,3}/.exec('candy')    // ["a", index: 1, input: "candy"]  /a{1,3}/.exec('caandy')   // ["aa", index: 1, input: "caandy"]  /a{1,3}/.exec('caaandy')   // ["aaa", index: 1, input: "caaandy"]  /a{1,3}/.exec('caaaandy')  // ["aaa", index: 1, input: "caaaandy"]  

[Xyz], [^ xyz]

[Xyz]: A Character Set combination. Matches any character in square brackets;

[^ Xyz]: A reverse character set. Match any characters that are not included in square brackets;

Both types of matching can use a break (-) to specify a character range. special characters do not have any special meaning in the character set.

Example:

Function escapeRegExp (string) {return string. replace (/([. * +? ^ =! :$ {} () | [\] \/\])/G, "\ $ &"); // $ & indicates the entire matched string}

In the example,. * +? ^ =! : $ {} () Indicates the literal volume, which has no special meaning.

Others

\ B: match the boundary of a word. The boundary of a matched word is not included in the matched content. In other words, the content length of the boundary of a matching word is 0;

\ B: match a non-word boundary;

Example:

  /\bm/.exec('moon')             // ["m", index: 0, input: "moon"]  /\bm/.exec('san moon')           // ["m", index: 4, input: "san moon"]  /oo\b/.exec('moon')             // null  /\B../.exec('noonday')          // ["oo", index: 1, input: "noonday"]  /y\B../.exec('possibly yesterday')    // /y\B../.exec('possibly yesterday')

\ D: matches a number, which is equivalent to [0-9].

\ D: match a non-numeric character, equivalent to [^ 0-9];

\ F: match a page feed (U + 000C );

\ N: match a line break (U + 000A );

\ R: match a carriage return (U + 000D );

\ S: matches a blank character, including spaces, tabs, page breaks, and line breaks, it is equivalent to [\ f \ n \ r \ t \ v \ u00a0 \ u1680 \ u180e \ u2000-\ u200a \ u2028 \ u2029 \ u202f \ u205f \ u3000 \ ufeff].

\ S: matches a non-blank character, it is equivalent to [^ \ f \ n \ r \ t \ v \ u00a0 \ u1680 \ u180e \ u2000-\ u200a \ u2028 \ u2029 \ u202f \ u205f \ u3000 \ ufeff];

\ W: match a single character (letter, number, or underline), equivalent to [A-Za-z0-9 _];

\ W: match a non-single character, equivalent to [^ A-Za-z0-9 _];

Regular Expression flag

G: Global Search;

I: case insensitive;

M: multi-row search;

Use Regular Expressions

RegExp has exec () and test () methods;

The exec matching result is: matching result, capture result, index and input.

The matching result of test is true or false, which is more efficient than exec.

String methods include match (), replace (), search (), and split;

The matching result is the same as the exec of RegExp. replace is replaced by the regular expression, search for the location, and split splits the string based on the regular expression.

When replace has a function, the parameters are described as follows:

* Match
* Memory items (items in brackets)
*...
* Matched index
* Input items

The above is the regular expression in js introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in time. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.