JavaScript Regular Expressions (notes) and javascript Regular Expressions

Source: Internet
Author: User

JavaScript Regular Expressions (notes) and javascript Regular Expressions

1. What is a regular expression?

// The regular expression is an object that describes the character pattern;
// JS defines the RegExp class to indicate a regular expression;
// Both String and RegExp define functions that use regular expressions for powerful pattern matching and text retrieval and replacement;

2. Create a regular expression

1. Create a regular expression
// JS provides two methods to create regular expressions. One is to use the new operator and the other is to use the literal method;
(1). var box = new RegExp ('box'); // The first parameter is a string;
Var box = new RegExp ('box', 'ig '); // The second parameter is an optional mode modifier;
(2). var box =/box/; // directly use two backslash;
Var box =/box/ig; // Add a pattern modifier;

2. RegExp object test Regular Expression
// The RegExp object contains two methods: test () and exec (). The functions are similar and used to test string matching;
(1). test (): Find whether a specified regular expression exists in the string and return a Boolean value;
// Test () instance
Var pattern = new RegExp ('box', 'I'); // create a regular expression;
Var str = 'this is a Box! '; // Create a string;
Alert (pattern. test (str); // use the test () method to verify the matching result;
// Use a statement to implement regular expression matching;
Alert (/box/I. test ('this is a box! '));
(22.16.exe c (): searches for the specified Regular Expression in the string. If the regular expression is successful, an array of related information containing the string is returned. If the regular expression fails, null is returned;
Exec () instance
Var pattern =/box/I;
Var str = 'this is a Box! ';
Alert(pattern.exe c (str); // an array is returned for matching;

3. String object test Regular Expression

(1). match (pattern): returns the substring or null in pattern;
// Obtain the matching Array Using the math () method;
Var pattern =/box/ig; // enable global
Var str = 'this is a Box !, That is a Box too! ';
Alert (str. match (pattern); // obtain the array: [Box, Box]
(2). search (pattern): returns the start position of pattern in the string;
Var pattern =/box/ig;
Var str = 'this is a Box !, That is a Box too! ';
Console. log (str. search (pattern); // 10; search () returns if it finds it; otherwise,-1 is returned;
(3). replace (pattern, replacement): replace pattern with replacement;
Var pattern =/box/ig;
Var str = 'this is a Box !, That is a Box too ';
Console. log (str. replace (pattern, 'Tom '); // replace Box with Tom;
(4). split (pattern): returns the array split by the specified pattern string;
Var pattern = // ig;
Var str = 'this is a Box !, That is a Box too .';
Console. log (str. split (pattern); // separate spaces to form an array;

3. Access Control

// Regular Expression metacharacters are special characters;
// They have some special functions to control the matching mode;
// The metacharacter after the backslash will lose its special meaning;

1. metacharacters/metacharacters matching
// Character class: Single Character and number
. Match any character except the line break;
[A-z0-9] matches any character in the character set in parentheses;
[^ A-z0-9] match characters that are not in the character set in parentheses;
\ D matches numbers;
\ D matches non-numbers;
\ W matches letters and numbers and _
\ W matches non-letters, numbers, and _
// Character class: white space characters
\ 0 matches null characters;
\ B matches space characters;
\ F matches paper input characters;
\ N matches the linefeed;
\ R matches the carriage return character;
\ T matches the tab;
\ S matches blank characters, spaces, tabs, and line breaks;
\ S matches non-blank characters;
// Character class: The Anchor character;
^ Match the beginning of the line;
$ Matching at the end of a row;
// Character class: repeated characters;
X? Matches 0 or 1 x;
X * matches 0 or any number of x
X + matches at least one x;
(Xyz) + match at least one (xyz );
X {m, n} matches at least m and at most n x;
// Character class: substitution character;
This | where | the logo matches this or any one of the where or logo;
// Character class: Record characters;
$1 matches the content in the first group;

Instance:
Pattern =/g.. gle/; // ".": match any character;
Pattern =/g. * gle/; // ". *": matches 0 or any number of characters;
Pattern =/g [a-z] * gle/; // [a-z] *: match any character in a-z;
Pattern =/g [^ 0-9] * gle/; // [^ 0-9] *: match any non-0-9 character;
Pattern =/[a-z] [A-Z] +/; // [A-Z] +: Match characters in the A-Z one or more times
Pattern =/g \ w * gle/; // \ w *: match any number of all characters and _;
Pattern =/google \ d */; // \ d *: match any number;
Pattern =/\ D {7,}/; // \ D {7,}: match at least 7 Non-numbers;
Pattern =/^ google $/; // "^": match from the beginning; "$": match from the end;
Var pattern =/8 (. *) 8 /;
Var str = 'this is 8google8 ';
Str. match (pattern );
Console. log (RegExp. $1); // obtain the string content in the first group;

2. Greed and inertia
++?
? ??
**?
{N }?
{N ,}{ n ,}?
{N, m} {n, m }?
Var pattern =/[a-z] +? /;//"? ": The greedy match is disabled and only the first one is replaced;
Var str = 'abcdefg ';
Alert (str. replace (pattern, 'xxx'); // => xxxdefg;

// Returns an Array Using exec
Var pattern =/^ [a-z] + \ s [0-9] {4} $/I;
Var str = 'Google 2015 ';
Alert(pattern.exe c (str) [0]); // returns the entire string => google 2015;

Var pattern =/^ [a-z] +/I;
Var str = 'Google 2015 ';
Alert(pattern.exe c (str); // returns the matched letter => google;

// Use special characters for matching;
Var pattern =/\. \ [\/B \]/;
Var str = '. [/B]';
Alert (pattern. test (str ));

// Use the line feed mode
Var pattern =/^ \ d +/mg;
Var str = '1. baidu \ n2.google \ n3.bing ';
Var result = str. replace (pattern ,"#");
Alert (result); // #. baidu #. google #. bing;

Four Common Regular Expressions

1. Check the zip code
// There are 6 digits in total. The first digit cannot be 0;
Var pattern =/[1-9] [0-9] {5 }/;
Var str = '20140901 ';
Alert (pattern. test (str ));

2. Check the compressed file package
// Number + letter + _ +. + zip | rar | gz
Var pattern =/[\ w] + \. zip | rar | gz /;
Var str = '123.zip ';
Alert (pattern. test (str ));

3. Remove unnecessary Spaces
Var pattern =/\ s/g; // must be global to match all;
Var str = '1970 111 222 ';
Var result = str. replace (pattern ,"");

4. Delete spaces at the beginning and end
Var pattern =/^ \ s +/; // force the first part;
Var str = 'Goo gl ';
Var result = str. replace (pattern, ""); // Delete the first space of a row;
Pattern =/\ s + $/; // force tail;
Result = result. replace (pattern, ""); // Delete spaces at the end of a row;
 
5. Simple email Verification
Var pattern =/^ ([a-zA-Z0-9 _\. \-] +) @ ([a-zA-Z0-0 _\. \-] + )\. ([a-zA-Z] {2, 4}) $ /;
Var str = 'abc123. com@gmail.com ';
Alert (pattern. test (str ));

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.