Regular Expressions in JS <<<

Source: Internet
Author: User
Tags alphabetic character

About Regular Expressions: 

  A regular expression is a logical formula for a string (including ordinary characters (for example, letters between A and z) and special characters (called "metacharacters"), which is a "rule string" that is composed of a predefined set of characters, and a combination of those specific characters, which Used to express a filtering logic for a string. A regular expression is a text pattern that describes one or more strings to match when searching for text.

Declaration of a regular expression

1, the literal declaration:

/Regular Expression rule/match pattern

Example:var reg =/^abc$/i;

2, the NEW keyword statement:

   var reg = new RegExp ("^abc$", I);

Ii. description of regular expression rules

  Rule description:

Symbol Describe
/.../ Represents the beginning and end of a pattern
^ Match the start of a string
$ Match the end of a string
\s Any whitespace character
\s Any non-whitespace character
\d Matches a numeric character, equivalent to [0-9]
\d Any character other than a number, equivalent to [^0-9]
\w Match a number, underscore, or alphabetic character, equivalent to [a-za-z0-9]
\w Any non-single character, equivalent to [^a-za-z0-9_]
. Any character other than a line break

quantifier Description:

Symbol Describe
N Matches the previous n times
{N,} Matches the previous item n times, or multiple times
{N,m} Match the previous item at least n times, but not more than m times
* Matches the previous item 0 or more times, equivalent to {0,}
+ Matches the previous item 1 or more times, equivalent to {1,}
? Matches the previous item 0 or 1 times, which means the previous item is optional

  Symbol:

Symbol Describe
| Match any one of the selected characters, such as P|q, to match Q or P
() Group
[] The data to match
^ Take counter

Common patterns of regular expressions

1,G means global match . No g indicates that only the first qualifying string is matched;

        "AAA". Replace (/a/, "*"); --"*AA"      

2,I means ignoring case matching , and the default case must also conform to the regular requirements.

    "AAa". Replace (/a/, "#"); --"a#a" "         AAA". Replace (/a/i, "#"), "#Aa"     "AAA". Replace (/a/gi, "#")--"# #"

3,m indicates multi-line matching mode:

If not with M, the string represents only one end of the beginning;

If you have M, you can have multiple endings at the beginning for multiple lines of string.

Example One:
varStr= "This is an\n antzone good"; varreg=/an$/;  Console.log (Str.match (reg)); Could not match string"An", although "an"The following line has been wrapped, but not a multiline match, so it is not the end of the string line. Example two:varStr= "This is an\n antzone good"; varreg=/an$/m; Console.log (Str.match (reg));
Can match string"An"because a multi-line match is used. Example three:varReg =/^b/;varstr = ' Test\nbbs '; Execreg (REG,STR);
The match failed because there is no B character at the beginning of the string. But after adding the M modifier: example four:varReg =/^b/m; varstr = ' Test\nbbs '; Execreg (REG,STR);
Match to B, because after the M modifier is added,^ has already indicated the beginning of the line, because BBS at the beginning of the second row of the string, so it can be successfully matched.

[ Multi-line string]

① string with \ n for line break: "Abc\nabc". Replace (/^A/GM, "*");

②ES6 can be used to denote a string, a string that supports direct line wrapping.

4. Detection method

(1)reg.test (str): Detects whether a string conforms to the regular requirement and returns TRUE or false.

(2)reg.exec (str): Detects whether a string conforms to the regular requirement, conforms to the returned array, and does not conform to return NULL.

Returns the format of the array:

①index Property: Represents the string, the first few characters, and begins to match the regular.

②input Property: Represents the complete retrieved string.

③ Subscript No. 0: Represents a string substring that conforms to a regular requirement.

④ subscript from 1: Represents a string substring that matches the () package in the regular. In other words, there are several () in the regular, and there are several subscripts in the returned array.

Example:

/12 (3) (\d) 56/.exec ("aaa123056bbb"); 

[      0: "123056", 1: "3", 2: "0", Index:3, input:< /c11> "AAA123056BBB", Length:3 ] 

Four, regular expressions commonly used validation exercises

1, verify the mailbox

var reg =/^[a-za-z0-9][email protected][a-za-z0-9]+\. [A-za-z]+ (\.[  a-za-z]{2,3})? $/;   var mail = "[email protected]";  console.log (reg.test (mail));

2. Verify ZIP code (6-bit)

var reg =/^\d{5}$/;   var mailno = "251400"; Console.log (Reg.test (Mailno));

3, verify the mobile phone number (the first bit is 1, the second is 3/5/7/8, a total of 11)

var reg =/^1 (3|5|7|8) \d{9}$/;   var phone = "17862009622"; Console.log (Reg.test (phone));

4. Verification Age (1~120)

var reg =/^ (\d|[  1-9]\d|1[01]\d|120) $/;  //  var reg =/^ ((1[0-1]|[ 1-9] \d|120) $/;  var age = "121"; Console.log (Reg.test (age));  

Regular Expressions in JS <<<

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.