Java Script Basics (12) Regular expressions

Source: Internet
Author: User
Tags alphabetic character

The symbols commonly used in regular expressions

Although you can use the string function to complete validation, this validation is not rigorous enough and is cumbersome to write. A regular expression is an object that describes a character pattern, consisting of special symbols that are used to match various expressions. Common symbols and usages are listed in the following table.

symbols and usages commonly used in regular expressions
symbols Description
 /....../   represents the beginning and end of a pattern.
 ^   Match string start  
 $   Match string end
 \s   any whitespace character  
 \s   Any non-whitespace characters & nbsp;
 \d   matches a numeric character equivalent to [0-9].  
 \d   matches any number other than the number, equivalent to [^0-9].  
 \w   matches a number, underscore, or alphabetic character equivalent to [a-za-z0-9_].  
 \w   matches non-single character, equivalent to [^a-za-z0-9_] 
.   Any character other than line breaks.

The characters in the previous table indicate which characters can be matched, and the number of characters that can occur is also indicated by a special symbol. As shown in the following index.

  

Repeating characters for regular expressions
Symbol Describe
N Matches the previous item n times.
{N,} Matches the previous item more than n times.
{N,m} Matches the previous item n times, but cannot exceed 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, equivalent to {0,1};

Through the above two tables, you can understand the role of some symbols of regular expressions, where "$", "+", "?", "*", "^", ".", "\" and other symbols are given special meanings, and if the characters themselves need to be matched in regular expressions, they need to be matched using the transfer character backslash "\". Like "\." Represents a "." Match, and \$ represents a "$" symbol.

Second, the RegExp object of the regular expression.

REGEXP (Regular expression, regular expressions) objects are powerful tools that perform pattern matching on strings when they are objects. There are two ways to define a regular expression, one is normal and the other is a constructor.

  1, Ordinary way:

var reg =/Expression/add-on function

In the syntax:

Expression: A string that specifies the pattern of the regular expression.

Additional parameters: used to extend the meaning of the expression, there are 3 main parameters:

G: The delegate can make a global match.

I: Represents a case-insensitive match.

M: Indicates that multiple lines can be matched.

The above 3 parameters can be any combination, representing the compound meaning, of course, can not add parameters.

Cases:

   var reg =/blue/;   var reg =/blue/gi;   var reg =/^\d{2,8}$/m;  var reg =/^\[email protected]\w+ (\.[ a-za-z]{2,3}) {1,2}$/;  // Email  

  2. How to construct a function

var reg = new RegExp (expression, additional parameters);

Where the meaning of the expression and additional parameters is exactly the same as above,

For example:

var New REGEXP ("Blue"); var New REGEXP ("Blue", "G");

If the expression is a regular expression instead of a string, "you can omit it."

var reg = new RegExp (/^\[email protected]\w+ (\.[ a-za-z]{2,3}) {1,2}$/);

  

Attention:

  An expression in a normal way must be a constant string, and the expression in the constructor can be a constant string, or it can make a JavaScript variable. For example, the parameters of a regular expression are based on the values entered by the User:

var reg = new RegExp (document.getElementById ("id"). Value, "G");

Three, string objects and regular expressions

In addition to regular expression methods that support RegExp objects, JavaScript supports regular expression methods for string objects, which define methods for using regular expressions to perform powerful pattern-matching and text-retrieval and substitution functions, which are commonly used in the following ways:

  1. Match ():

The match () method can retrieve the specified value within the string and find the match of one or more regular expressions.

Grammar:

String object. Match (searchstring or regexpobject);

SearchString is the value of the string to retrieve

Regexpobject is the RegExp object that specifies the pattern to match.

For example:

var str = "My cat"; var reg =/cat/; var result=str.match (REG);

The value of result is: Cat.

  2. Search ():

The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression. The method does not perform a global match, returns the first matching position of the substring, and returns 1 if no matching substring is found, similar to the indexof () method of String.

Grammar:

String object. Search (searchstring or regexpobject);

SearchString is the value of the string to retrieve

Regexpobject is the RegExp object that specifies the pattern to match.

For example:

var str = "Hello rock!rock!" ; var result = Str.search (/rock/);

The value of result is: 6;

  3.replace ():

The Repleace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.

Grammar:

String object. Replace (RegExp object receives string, "replaced string")

If full-text indexing is set, the eligible regexp or strings are replaced, or only the first one is replaced, and the replaced string is returned, for example:

var str = "My little white cat,is really a very lively cat"; var result = Str.replace (/cat/, "dog"); var results = Str.reaplace (/cat/g, "dog");

Value of Result: My Little white dog,is really a very lively cat

Results value: My Little white dog,is really a very lively dog

 4. Split ():

The split () method splits a string into a series of substrings and returns the series of substrings through an array.

Grammar:

A String object. Split (delimiter, N);

A delimiter can make a string, or you can make a regular expression. N is an option to limit the number of output arrays, and if n is not set, an array of elements containing the entire string is returned, for example:

   var str = "Red,blue,green,white";   var result = Str.split (",");   var string = "";   for (var i = 0;i<result.length;i++) {string+=result[i] + "_"; }
document.write (string);

The result is:

Red_blue_green_white_;

  

  

  

Java Script Basics (12) Regular expressions

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.