Learn JavaScript regular expression _ javascript skills

Source: Internet
Author: User
This article mainly introduces JavaScript Regular Expressions and provides a detailed understanding of JavaScript regular expressions to better understand JavaScript Regular Expressions. If you are interested, refer JavaScript Regular Expression learning:

There is a regular expression debugging tool online. All the following sample code can be viewed on codepen.

1. Create a regular expression

Var re =/AB + c/; // method 1 Regular Expression literal var re = new RegExp ("AB + c"); // method 2 RegExp object constructor

1) The regular expression literal is compiled after the script is loaded. If your regular expression is a constant, you can use this method to achieve better performance.

2) Use constructors to compile regular expressions during runtime. When you know that the regular expression pattern will change, or you do not know its pattern in advance or from other places (such as user input ), the obtained code is suitable for constructor.

2. special characters in Regular Expressions

\ ^ $ * +? . (X )(? : X) x (? = Y) x (?! Y) x | y {n}

{N, m} [xyz] [^ xyz] [\ B] \ B \ B \ cX \ d \ D \ f \ n \ r

\ S \ S \ t \ v \ w \ W \ n \ 0 \ xhh \ uhhhh

3. Methods in Regular Expressions

There are 6, which areExec, test, match, search, replace, and split.

The exec and test syntaxes are all called by regexObj, while the match, search, replace, and split syntaxes are all called by string.

Exec:

Method is used to search for a specified string. Its return value is an array or null. The syntax is as follows: regexObj.exe c (str)

Sample Code:

Var re =/quick \ s (brown). +? (Jumps)/ig;
Var result = re.exe c ('the Quick Brown Fox Jumps Over The Lazy dog ');
Returned results:

Test:

Returns true or false for a RegExp method that tests matching strings. The syntax is as follows: regexObj.exe c (str)

Match:

Returns an array or null if no matching result is returned.

Unlike exec, math is called by strings, while exec is called by RegexObj.

Second, if the expression contains the "g" mark, a matched string array is returned. If the expression does not exist, it will be the same as the string array returned by exec. The demo below contains "g ". Syntax: str. match (regexp)

Sample Code:

var re = /quick\s(brown).+?(jumps)/ig;var result = re.exec('The Quick Brown Fox Jumps Over The Lazy Dog');

Returned results:

Search:

A String method used to test matching in a String. It returns the matched Location index or-1 if it fails. Syntax: str. search (regexp)

For example, if the preceding sample code calls search, the returned data is 4.

Replace:

A String method is used to search for matched strings and replace the matched substring with a replacement String. Syntax: str. replace (regexp | substr, newSubStr | function [, flags])

var re = /(\w+)\s(\w+)/;var str = "John Smith";var result = str.replace(re, "$2, $1");

The returned result is "Smith, John"

Split:

A regular expression or a fixed String is used to separate a String, and the separated substring is stored in the String method in the array. Syntax: str. split ([separator [, limit])

Limit is used to limit the number of split arrays. The following is a demo, but one of the expressions is enclosed in parentheses. If one is not added, the returned data is different.

Sample Code:

var re = /(\d)/;var result = 'Hello 1 word. Sentence number 2.'.split(re);console.log(result);var re = /\d/;var result = 'Hello 1 word. Sentence number 2.'.split(re);console.log(result);

Returned results:

4. Information returned by regular expression execution

var myRe = new RegExp("d(b+)d", "g");var myArray = myRe.exec("cdbbdbsbz");console.log(myArray);

The result returned by the Code is as follows:

5. Regular Expression mark

Var re =/\ w + \ s/g; // expression var re = new RegExp ("\ w + \ s", "g "); // expression 2 var str = "effecfi fo fum"; var myArray = str. match (re); console. log (myArray );

Expression 1 and expression 2 return the same result. Are the following Arrays:

The above is all the content of this article, hoping to help you learn.

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.