"JavaScript" Regular expressions

Source: Internet
Author: User

The regular expression was originally in the machine room to reconstruct the time to understand, then look at the blog has partners with regular expression on the input box content restrictions, the use of those characters but do not understand, only know that can achieve that effect, When you learn the ASP. NET validation control and use it again, this time learning JavaScript to understand it again, then let's look at the regular expression.
first, what is a regular expression

    Regular expression (regular expression) is an object that describes the character pattern. ECMAScript's RegExp class represents regular expressions, and both string and RegExp define powerful pattern-matching and text-retrieval and substitution functions using regular expressions. Baidu interpreted as regular expression formal notation , General notation, regular expressions are pairs of characters

second, why use regular expressions

Regular expressions are primarily used to validate client input data (like number, name, gender, phone, mailbox, etc.). After the user fills out the click button, the information will be sent to the server, before committing to the server will be first verified, the regular expression in the program is mainly used to verify the client input data , can save a lot of server-side system resources, and can provide a better user experience.

Third, create

There are two ways to create a regular expression similar to creating a string:

1. Through the new operator

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" >var box=new RegExp (' box ', ' IG ');</span></span>

2. The literal Way

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" >var box=/box/ig;</span></span>

Iv. testing Regular Expressions

The RegExp object contains two methods: Test () and exec (), which are basically similar in function for testing string matching.

1.test () method

The test () method looks in the string for the existence of the established regular expression and returns a Boolean value, returns True if it exists, or false if it does not exist.

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" >var pattern=new RegExp (' box ', ' I ');//create regular expressions, case-insensitive var str= ' This is a box! ';         Create a String alert (Pattern.test (str)) to be aligned;         Verify that the match is matched by the test () method </span></span>

To run the pop-up prompt box:

2.exec () method

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

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" >/* returns a matching array using exec */var pattern=/vivian/i;var str= ' I am vivian! '; Alert (pattern.exec (str));     Matches the returned array, otherwise returns null</span></span>
To run the pop-up prompt box:


v. Methods of using regular expressions of strings


Take the Replace () method as an example:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:24PX;" >/* returns a matching array using exec */var pattern=/vivian/ig;var str= ' I am Vivian!,she is Vivian too '; alert (str.replace (pattern, ' Lily ') );     Replace the Vivian with the lily</span></span>
display after running:

Vi. access ControlRegular expression metacharacters are characters that contain special meanings, and they have special features that control how the pattern is matched.

Take mailbox verification as an example:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >var pattern=/^ ([\w\.\-]+) @ ([\w\-]+) \. ([\w]{2,4}) $/;var str= ' [email protected] '; Alert (Pattern.test (str));</span>


Greed and Inertia

Greed: First look at the entire string is a match, if not match the last string is removed and then matched, from right to left, until a match is found or no word left characters stop.

Lazy: First See whether the first string matches, if not match then look at the second, and so on from left to right, and greedy quantifier in the opposite direction.             

1. Greedy mode:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >var pattern=/[a-z]+/;var str= ' abcdefj '; alert (str.replace (Pattern, ' 1 '));</span>
all strings become 1, and the last run result is: 1

2. Lazy Mode:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >var pattern=/[a-z]+? /;var str= ' ABCDEFJ '; alert (str.replace (Pattern, ' 1 '));</span>
only the first character becomes 1, there is no match, and the result is: 1BCDEFJ

Summary:

Original Study ASP validation of the control when there is an e-mail verification, selected after you will see a large number of letters and numbers and symbols of the thing, then was thinking what this is a ghost, and now learn JS before I knew it was like this, ASP The regular expressions have been encapsulated, and we don't need to write them ourselves.

This blog is just to introduce some basic foundation, write examples are relatively simple, this blog as a primer. The regular expression is really strong, we can write things that meet our requirements, but it has a lot of matches, and now I'm just fur on them, if I really need to know more to write.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"JavaScript" 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.