A regular expression was contacted in ASP. NET, which was already encapsulated in the editor. This time in JS again contact, feel understanding is not very in place, it is necessary to sum up a bit. Through regular expression can save a lot of conditions to judge, it is very convenient to use.
When writing a program or Web page that handles strings, there is often a need to find strings that match certain complex rules. The regular expression is used to describe
it the tools of the rule. The expression is used primarily to validate the client's input data. and other server scripts to further process ASP.
Service The terminal is usually used, and is sent to the server because of client authentication, can save a lot of server-side system resources, and provide better use
The user Body test. In other words, the regular expression is the code that records the text rule. For example, we usually register things on the Internet when the mailbox format of the sentence
off, the user The judgment of the name and the judgment of the telephone number and the ID number are judged by regular expressions.
(1) Two methods related to regular expressions
1) The Replace () method returns the copy of the string after the literal substitution based on the regular expression.
Syntax: Stringobj.replace (Rgexp,replacetext)
2) The match () method uses the regular expression pattern to perform a lookup on a string and returns the result that contains the lookup as an array.
Syntax: Stringobj.match (RGEXP)
(2) Two ways to create regular expressions
Similar to creating a regular expression and creating a string, creating a regular expression provides two methods, one is the new operator and the other is the literal way
1) var box = new RegExp ("box", "IG");
2) var box =/box/ig;
The regular expression is written between the two double slashes, the last double slash is followed by G, and the double slash is written between the strings that you want to find. g represents the meaning of global
Thinking is global. Of course, if the content you want to find is not case-sensitive, add the I letter after G. M stands for multiline matching.
To get a good understanding of regular expressions, be sure to do more examples and understand them in an example.
var pattern =/[1-9][0-5]{5}/; Zip code matches var str = "This is 224000"; Alert (Pattern.test (str)); var pattern =/^[\w]+\. (zip|gz|rar)/; Check the file's compression package var str = "S_.zip"; Alert (Pattern.test (str)), VAR pattern =/^ ([\w\.\-]+) @ ([\w\-]+] \. ([a-za-z]{2,4}$)/; Email verification var str = "[email protected]"; Alert (Pattern.test (str));
The following gives this website to the regular expression grammar rules and so on detailed introduction, hoped has the help to everybody http://msdn.microsoft.com/zh-cn/library/28hw3sce (v=vs.80). aspx
Regular expressions should be used in the future. We can usually consciously accumulate some common code, such as verifying the birth certificate number, postal
box, phone number, etc. is correct, to form their own code base, in the future to use the back to check on it.
JS Basic Series--Regular expression