The regular expression is not the first time to meet with it. When we were learning ASP. NET video, there was a reference to that instance of the validation control. At that time, are set in the properties of the control itself, with the principle is regular expression, then feel is convenient, strong, brought us a great convenience. This time, it is a deep study of regular expressions. I. What is a regular expressionRegular Expressions (regular expression) are an object that describes the character pattern. The RegExp class of ECMAScript represents a regular expression. regular expressions are mainly used tovalidating the client's input data. Because of client authentication, you can save a lot of server-side system resources and provide a better user experience. Suppose the user needs to fill in the HTML form with name, address, date of birth, etc. Then, before submitting the form to the server for further processing, the JavaScript program examines the form to confirm that the user has entered the information and that the information is compliant. two. Create a regular expressionthe creation of regular expressions provides two methods, one with the new operator and one in the literal way. 1. Two ways to create var box = new RegExp (' box '); //new method var box =/box/; Method of literal 2. The test regular expression RegExp object contains two methods for testing string matching, test(), and exec (). The test () method finds in the string whether a regular expression is present and returns a Boolean value, returns True if it exists, no return false, andthe Exec () method is also used to find a regular expression in a string. If execution succeeds, returns an array of related information containing the lookup string, execution fails, and NULL is returned. 4 ways to use regular expressions with 3.String objects
- Match method gets the matching array
- Replace method replaces the data that is matched to
- Search method finds matching array
- Split method splits into a string array
three. Get Controlfrom the name of the light, it is not clear what is going to be said here. So go straight from the following example and implementation to see what it is. Example 1:var pattern =/g. gle/; Dot symbol to match any character except line breakvar str= ' g12gle ';Alert (pattern.test (str)); The returned result is trueExample 2:var pattern =/go*gle/; O*, representing 0, 1, or more Ovar str= ' gbbbgle ';Alert (pattern.test (str)); The returned result is falseExample 3:var pattern =/g.? gle/; .?, which represents any character of 1, or 0var str= ' gbbgle ';Alert (pattern.test (str)); The returned result is trueso it seems that getting control is actuallyVerify that you can match the string by using a variety of special characters. Because some information verification is not unique, for example, we verify that the user's contact information is reasonable, we can not write the contact method is dead, each user's information is not the same, at this time, it is necessary to use a variety of characters, the meaning of the character expanded, so that to match the user's message becomes flexible. Here are some of the most commonly used regular in this section of the content, from the example, it may be more clear.
four. Common regular1. Check the ZIP codevar pattern =/[1-9][0-9]{5}/; A total of six digits, the first digit cannot be 0var str = ' 224000 ';Alert (pattern.test (str)); The returned result is true2. Check the file compression packagevar pattern =/[\w]+\.zip|rar|gz/; \w denotes all numbers and letters underlinedvar str = ' 123.zip '; \. Indicates a match, followed by a selectionAlert (pattern.test (str)); The returned result is true
Five. Learning experiencethe regular expression of learning, before the depth of understanding, the experience of its powerful. So, to a large extent stimulate their interest in learning, very much want to do in-depth understanding. In the course of learning this part of the process, in general, there is no difficulty, some do not understand through the example of their own to verify, to match, also solved. Regular expression, very powerful, very convenient, very like.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"JavaScript" Regular expressions