javascript--Regular Expressions

Source: Internet
Author: User

Regular expressions (regular expression) are an object that describes the character pattern. The RegExp class of JavaScript represents regular expressions, and both String and REGEXP define powerful pattern-matching and text-retrieval and substitution functions using regular expressions

regular expressions are primarily used to validate the input data of the client. After the user fills out the form by clicking the button, the form is sent to the server, which is usually further processed on the server side. Because of client authentication, it can save a lot of server-side system resources and provide a better user experience.

To create a regular expression

There are two ways to create a regular expression

One is to use the new operator

var New // first argument string var New // second parameter optional pattern modifier

Another is the use of the literal method (more recommended)

var // Direct use of two backslashes var // Add a pattern modifier after the second slash
To test the regular expression

The 1.RegExp object contains two methods: Test() and exec ()for testing string matches

The test () method finds in the string whether the specified regular expression exists and returns a Boolean value, returns True if it exists, or false if it does not exist

The exec () method is also used to find the specified regular expression in a string, and if the Exec () method succeeds, returns an array of related information containing the lookup string. If execution fails, NULL is returned

Text () method

var // Create regular mode, case insensitive var str = ' This is a box! ' ; alert (Pattern.test (str));

Or use a statement to implement a regular match

// pattern and string replaced two variables

EXEC () method

var pattern =/box/i; var str = ' This is a box! '  // matches the returned array, otherwise returns null

The 2.String object also provides 4 ways to use regular expressions

Replace matches to return the replaced string, otherwise the original string is returned

Get Control

A regular expression meta-character is a character that contains special meanings. They have some special features that control how the pattern is matched. Metacharacters after the backslash will lose its special meaning

There are red lines in the classification of the Red Line is more commonly used, no red line classification is more commonly used. The metacharacters of these classifications can be used together, for special characters (such as-,., etc.) to be escaped with "\"

var pattern =/[a-za-z0-9]oogle/;        // [a-za-z0-9] indicates matching case a-za-z0-9 var str = ' oogle '; alert (Pattern.test (str)); var pattern =/[^a-za-z0-9]oogle/;                // [^0-9] represents any character that is not 0-9 var str = ' _oogle '; alert (Pattern.test (str)); var pattern =/^[0-9]oogle/;                // this ^ symbol, which is added in/behind instead of [], indicates that it starts with 0-9 and can be seen  in the anchor character . var str = ' 444oogle '; alert (Pattern.test (str));

var pattern =/goo\sgle/;            // \s indicates a space match var str = ' goo gle '; alert (Pattern.test (str));

var pattern =/^[a-z]oogl[0-9]$/;        // ^ Force first match, $ force tail match             var str = ' GOOGL5 '; alert (Pattern.test (str));

varpattern =/g. gle/;//dot symbol to match any character except line breakvarstr = ' G12gle '; alert (Pattern.test (str));varpattern =/go*gle/;//o*, representing 0, 1, or more Ovarstr = ' Gbbbgle '; alert (Pattern.test (str));varpattern =/go+gle/;//o+, which represents 1, or morevarstr = ' Ggle '; alert (Pattern.test (str));varpattern =/go?gle/;//O, representing 1, or 0varstr = ' Google '; alert (Pattern.test (str));varpattern =/g.? gle/;//.?, which represents any character of 1, or 0varstr = ' Gbbgle '; alert (Pattern.test (str));varpattern =/go{2,4}gle/;//o{2,4} indicates matching o2-4 times, including 2 and 4varstr = ' Google '; alert (Pattern.test (str));varpattern =/go{3}gle/;//o{3}, which means only 3 can be qualifiedvarstr = ' Goooogle '; alert (Pattern.test (str));varpattern =/go{3,}gle/;//O{3,}, expressed as 3 or more than 3varstr = ' Goooooooooooooooooooogle '; alert (Pattern.test (str));

After grouping using n (), you can use $n to get the contents of the group

greedy mode and lazy mode

The default is greedy mode, which means that all matching characters will be matched, while lazy mode matches only the first matching character

Just add one behind where you need to turn on the inertia? It's all right.

Example1. Check the special number string
var box=/[1-9][0-9]{5}/; var str= ' This is a 220400 ';            // must be 6 bits, the first digit cannot be 0, must be the number alert (box.test (str));
2. Check the file name
var box=/^[\w\-]+\. (ZIP|RAR|GZ)/; var str= ' 123-lz.zip ';                 // Check the file format to. zip,rar,gz for the extended name alert (box.test (str));
3. Remove extra spaces
var box=/\s/G; var str= ' 111 222 AAA BBB ';            // Remove the Space alert (str.replace (box, '));
4. Remove the leading and trailing spaces
varbox=/^\s+/;//the more stupid wayvarStr= ' goog le ';//remove the leading and trailing spaces without removing the middle spacevarResult=str.replace (Box, "); Box=/\s+$/; result=result.replace (Box, "); alert (| +result+ ' | ');varbox=/^\s+ (. +?) \s+$/;//use lazy (non-greedy) mode to combine with exec to get a matching arrayvarStr= ' goog le ';//remove the leading and trailing spaces without removing the middle spacevarResult=box.exec (str) [1];alert (| +result+ ' | ');varbox=/^\s+ (. +?) \s+$/;//use lazy (non-greedy) mode to replace with string.replace (. +?) of somethingvarStr= ' goog le ';//remove the leading and trailing spaces without removing the middle spaceAlert (' | ') +str.replace (box, ' $ ') + ' | ');
5. Simple Email Verification
var box=/^ (^[a-za-z0-9].+) @ ([\w\-]+) \. ([a-za-z0-9]{2,4}) $/; var str= ' [email protected] ';                    // Simple Verification Mailbox alert (box.test (str));

javascript--Regular Expressions

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.