JS Regular expression of the use of detailed _javascript skills

Source: Internet
Author: User

The regular expressions in JS are a lot weaker than regular expressions in C #, but they're pretty much enough.
1 Defining regular expressions
2 about the validation of the three-this expression method
3 escape characters for regular expressions

1 Defining regular expressions
The definition of regular expressions in JS is simple, there are two ways, one is through the constructor, one is through//, that is, two slashes.
For example

Copy Code code as follows:

var re =new RegExp ("\" \ \\w{1,}=\\w{1,}&) {1,}\\w{1,}=\\w{1,} ");

Using a constructor to define a regular expression, note that it is case insensitive, and the responsibility will not work. Because the parameter of the constructor is a string, or it can be defined as a two slash, some special characters are encountered with \ to escape
Define the same regular expression by means of a double slash
Copy Code code as follows:

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

Copy Code code as follows:

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

can achieve the same effect as the constructor, but careful analysis reveals that more escape characters are required through constructors \

2 three regular expression methods on validation

The main string method of using regular expressions match, the method of regular expression Exec,test
The regular expression method test tests whether a given string satisfies a regular expression, and the return value is of type bool, only true and false, and if it is simply a judgment that does not require additional processing, it can be used especially when validating.
Copy Code code 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 a given string satisfies a regular expression. Returns a matching string, if no match is returned null, and test is basically consistent, if you need to get the matching substrings, you can use the subscript, the example of the top test can be rewritten as follows
Copy Code code 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.exec (text);
if (result)
{
Alert ("OK");
alert (result); It's?test=1&ww=2&www=3,ww=2&.
Alert (result[0]+ ", 0");/yes? test=1&ww=2&www=3
Alert (result[1]+ ", 1");/Is ww=2&
}else
{
Alert ("Err");
}

}

Match is actually a string method, but the argument is a regular expression, the above example rewrite, as follows
Copy Code code 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, and so on, but these methods are no longer suitable for validation.
Copy Code code 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 escape characters for regular expressions
Escape characters often appear in regular expressions, such as question marks? There is a special meaning in regular expressions, if you need to match the question mark? You need to escape, use the escape character back slash \
The following two are a string that matches the beginning of the question mark
Copy Code code as follows:

function Test () {
var text= "test=1&ww=2&www=3";
var re =/^\? (\w{1,}=\w{1,}&) {1,}\w{1,}=\w{1,}/;//\? Represents the configuration question mark?
var re =new RegExp ("^\\?") ( \\w{1,}=\\w{1,}&) {1,}\\w{1,}=\\w{1,} ");/\ \ = Configuration question mark?
var result= re.exec (text);
if (result)
{
Alert ("OK");
alert (result);
Alert (result[0]+ ", 0");
Alert (result[1]+ ", 1");
}else
{
Alert ("Err");
}

}

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.