Learn JavaScript Regular expression _javascript skills

Source: Internet
Author: User

JavaScript regular-expression learning:

There is a regular tool for online debugging. All of the following sample code can be found on the Codepen.

1. Creating Regular Expressions

var re =/ab+c/; Way one regular expression literal number

var re = new RegExp ("Ab+c");//Mode two RegExp object's constructor

1 The regular expression literal is compiled after the script is loaded. If your regular expression is constant, you can get better performance with this approach.

2 The constructor is used to provide compilation of the runtime of the regular expression. When you know that the pattern of a regular expression changes, or that you don't know the pattern in advance or from another place (such as a user's input), the resulting code is better suited to the 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 \ r \ n

\s \s \ \v \w \w \ n \xhh \uhhhh

3. Methods in regular expressions

There are 6, respectively, exec, test, match, search, replace, and split.

Both the exec and test syntax are regexobj calls, and match, search, replace, and split syntax are string calls.

Exec:

method to perform a search matching operation for a specified string of strings. Its return value is an array or null. The syntax is as follows:regexobj.exec (str)

Sample code:

var re =/quick\s (brown). +? (jumps)/ig;
var result = Re.exec (' The Quick Brown Fox jumps over the Lazy Dog ');
return Result:

Test

A RegExp method that tests whether a match is in a string and returns True or false. The syntax is as follows:regexobj.exec (str)

Match

A RegExp method that performs a lookup match in a string that returns an array or returns NULL when it does not match.

Slightly different from Exec, the first is the calling method, where math is called by a string, and exec is called by Regexobj.

Second, if there is a "G" tag in the expression, then a matching string array is returned, and if not, it will be the same as if it were returned by exec. The following demo is a "G". The syntax is as follows: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 ');

return Result:

Search

A string method that tests a match in the strings, returns the index of the location to which it is matched, or returns 1 if it fails. The syntax is as follows:Str.search (regexp)

For example, if the example code above is called search, the return data is 4.

Replace

A string method that performs a lookup match in a string and replaces the substring that was matched with a replacement string. The syntax is as follows:str.replace (Regexp|substr,newsubstr|function[,flags])

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

The result returned would be: "Smith, John."

Split

A string method that separates a string using a regular expression or a fixed string and stores the delimited substring in an array. The syntax is as follows:str.split ([Separator[,limit]])

Limit is the limit of the number of partitioned arrays. The following is a demo, but one of the expressions is a parenthesis, a no plus, the returned data is not the same.

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);

return Result:

4. Regular expression execution return information

var Myre = new RegExp ("D (b +) d", "G");
var myarray = myre.exec ("Cdbbdbsbz");
Console.log (myarray);

The results returned in the code are as follows:

5. Regular Expression Flags

var re =/\w+\s/g;//expression one
var re = new RegExp ("\\w+\\s", "G");//Expression two
var str = "fee fi fo fum";
var myarray = Str.match (re);
Console.log (myarray);

Expression One and expression two return the same result. Are all of the following arrays:

  above is the whole content of this article, I hope to help you learn.

Related Article

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.