Use of js Regular Expressions _ javascript tips-js tutorial

Source: Internet
Author: User
This article provides a detailed analysis of the use of regular expressions in Javascript. For more information, see The Regular Expression in js is much weaker than the regular expression in C #, but it is basically enough.
1. Define a regular expression
2. Three Methods of this expression for verification
3. Regular Expression escape characters

1. Define a regular expression
Defining a regular expression in js is simple. There are two methods: constructor and slash.
For example

The Code is as follows:


Var re = new RegExp ("\\? (\ W {1 ,}=\\ w {1 ,}&) {1 ,}\\ w {1 ,}=\\ w {1 ,}");


Use the constructor to define a regular expression. Note the case sensitivity. Because the constructor parameter is a string or can be defined by two slashes, escape is required when encountering some special characters.
Define the same regular expression using a double slash.

The Code is as follows:


Var re = /\? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/;


The Code is as follows:


Var re = new RegExp (/^ \? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/);


It can achieve the same effect as the constructor, but after careful analysis, it is found that more escape characters \

2. Three Regular Expression Methods for verification

The regular expression mainly includes the string method match, the regular expression method exec, test
The regular expression method test is used to test whether the given string meets the regular expression. The return value is of the bool type and only true or false. If it is a pure judgment, no other processing is required, it can be used, especially during verification.

The Code is as follows:


Function test (){
Var text = "index. aspx? Test = 1 & ww = 2 & www = 3 ";//
Var re = /\? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/;
// Var re = new RegExp ("\\? (\ W {1 ,}=\\ w {1 ,}&) {1 ,}\\ w {1 ,}=\\ w {1 ,}");
Var result = re. test (text );
If (result)
{
Alert ("OK ");
} Else
{
Alert ("err ");
}

}


The regular expression method exec tests whether the given string meets the Regular Expression and returns the matched string. If no match exists, null is returned, which is basically the same as test, if you want to obtain the matched substrings, you can use the following method to rewrite the above test example:

The Code is as follows:


Function test (){
Var text = "index. aspx? Test = 1 & ww = 2 & www = 3 ";
Var re = /\? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/;
// Var re = new RegExp ("\\? (\ W {1 ,}=\\ w {1 ,}&) {1 ,}\\ w {1 ,}=\\ w {1 ,}");
Var result = re.exe c (text );
If (result)
{
Alert ("OK ");
Alert (result); // yes? Test = 1 & ww = 2 & www = 3, ww = 2 &
Alert (result [0] + ", 0"); // yes? Test = 1 & ww = 2 & www = 3
Alert (result [1] + ", 1"); // It is ww = 2 &
} Else
{
Alert ("err ");
}

}


Match is actually a string method, but the parameter is indeed a regular expression. After the above example is rewritten

The Code is as follows:


Function test (){
Var text = "index. aspx? Test = 1 & ww = 234 ";//
Var re = /\? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/;
// Var re2 = "(\ w {1, }=\\ w {1, }&) {1, }\\ w {1 ,} =\\ w {1 ,}"
Var result = text. match (re );
If (result)
{
Alert (result );//? Test = 1 & ww = 234, test = 1 &
Alert (result [0] + ", 0 ");//? Test = 1 & ww = 234
Alert (result [1] + ", 1"); // test = 1 &
} Else
{
Alert ("err ");
}
}


In fact, there are multiple functions in the string class that can pass regular expressions, split, search, replace, etc., but these methods are not suitable for verification.

The Code is as follows:


Function test (){
Var text = "index. aspx? Test = 1 & ww = 234 ";//
Var re = /\? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/;
// Var re2 = "(\ w {1, }=\\ w {1, }&) {1, }\\ w {1 ,} =\\ w {1 ,}"
Var result = text. split (re );
Alert (result );
Alert (result [0] + ", 0 ");
Alert (result [1] + ", 1 ");
}


3. Regular Expression escape characters
In regular expressions, escape characters, such as question marks? The regular expression has a special meaning. If you need to match the question mark? Escape is required, and the Escape Character backslash is used.
The following two strings match the strings starting with the question mark.

The Code is as follows:


Function test (){
Var text = "? Test = 1 & ww = 2 & www = 3 ";
Var re =/^ \? (\ W {1 ,}=\ w {1 ,}&) {1 ,}\ w {1 ,}=\ w {1 ,}/;//\? Indicates the configuration question mark?
// Var re = new RegExp ("^ \\? (\ W {1 ,}=\\ w {1 ,}&) {1 ,}\\ w {1 ,}=\\ w {1 ,}"); //\\? Indicates the configuration question mark?
Var result = re.exe c (text );
If (result)
{
Alert ("OK ");
Alert (result );
Alert (result [0] + ", 0 ");
Alert (result [1] + ", 1 ");
} Else
{
Alert ("err ");
}

}

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.