Regular Expression (above)
Content outline: 1. What is a regular expression 2. Create a regular expression for reprinting, please specify the source, thank you! Assume that you need to enter the name, address, and date of birth in the HTML form. Before submitting the form to the server for further processing, the JavaScript program checks the form to confirm that the user has entered the information and the information is compliant. In this case, a regular expression is required. 1. What is a regular expression (regular expression) is an object that describes the character pattern. The RegExp class of ECMAScript represents a regular expression, while both String and RegExp define functions that use regular expressions for powerful pattern matching and text retrieval and replacement. Regular Expressions are mainly used to verify the input data of the client. After you fill out the form and click the button, the form will be sent to the server. on the server side, the form will be processed by PHP, ASP. NET, and other server scripts. Because client-side verification can save a lot of system resources on the server side and provide a better user experience, the client can use regular expressions for verification before being submitted to the server for processing. 2. Creating a regular expression is similar to creating a string. Two methods are provided to create a regular expression. One is using the new operator, and the other is using the literal method. 1. var box = new RegExp ('box') using the new operator; // The first parameter string var box = new RegExp ('box', 'ig '); // optional parameter parameters of the second parameter (optional mode modifier) the number of optional parameter parameters of the modifier (I) the case-insensitive g Global Match m multi-row match is ignored 2. use the literal var box =/box/; // directly use two backslash var box =/box/ig; // Add the pattern modifier 3 after the second slash. the RegExp object of the test regular expression contains two methods: test () and exec (), which have similar functions and are used to test string matching. The test () method finds whether a specified regular expression exists in the string and returns a Boolean value. If yes, true is returned. If no regular expression exists, false is returned. The exec () method is also used to find the specified Regular Expression in the string. If the exec () method is successfully executed, an array containing the relevant information of the query string is returned. If the execution fails, null is returned. RegExp object method function test in the string test mode matching, returns true or false exec in the string to perform a matching search, returned result array/* example of the test method using the new operator */var pattern = new RegExp ('box', 'I'); // create a regular mode, case Insensitive var str = 'this is a Box! '; // Create the alert (pattern. test (str); // use the test () method to verify whether the match exists./* use the literal test method example */var pattern =/box/I; // create a regular expression, Case Insensitive var str = 'this is a Box! '; Alert (pattern. test (str);/* use a statement to implement regular expression matching */(it looks messy and is generally not recommended !) Alert (/box/I. test ('this is a Box! '); // The mode and string are replaced with two variables./* use exec to return a matching array */var pattern =/box/I; var str = 'this is a Box! '; Alert(pattern.exe c (str); // The returned array is matched; otherwise, the null PS: exec method is returned and other specific applications are returned. After obtaining control, we can read it again (see the next article ). 4. in addition to the test () and exec () methods, the String object also provides four methods to use the regular expression (using methods with est () and exec () method ). The regular expression method in the String object contains the expression pattern and returns the substring or null replace (pattern, replacement) in pattern to replace pattern search (pattern) with replacement) returns the array of the string that is split by the specified pattern by the start position split (pattern)./* obtains the matching Array Using the match Method */var pattern =/box/ig; // global search var str = 'this is a Box !, That is a Box too'; alert (str. match (pattern); // match to two: Box, Boxalert (str. match (pattern ). length); // get the length of the array/* use search to find matching data */var pattern =/box/ig; var str = 'this is a Box !, That is a Box too'; alert (str. search (pattern); // find the first and then return the starting position; otherwise, return-1 PS: because the search method is found, return, that is, no g Global is required. /* Replace the matched data with replace */var pattern =/box/ig; var str = 'this is a Box !, That is a Box too'; alert (str. replace (pattern, 'Tom '); // replace the Box with Tom/* split it into a string array */var pattern = // ig; // separate var str = 'this is a Box !, That is a Box too'; alert (str. split (pattern); // separate spaces to form static attributes of the array RegExp object (not useful) attribute short name semantic input $ _ currently matched string lastMatch $ & last matched string lastParen $ + matched substring leftContext In the last pair of parentheses $ 'before the last match the substring multiline $ * is used to specify whether all expressions are used for the Boolean value rightContext $ 'substring after the last match/* use static attribute */var pattern =/ (g) oogle/; var str = 'this is google! '; Pattern. test (str); // first execute alert (RegExp. input); // This is google! Alert (RegExp. leftContext); // This is alert (RegExp. rightContext );//! Alert (RegExp. lastMatch); // google alert (RegExp. lastParen); // g (group, Parentheses, later) alert (RegExp. multiline); // false PS: Opera does not support the input, lastMatch, lastParen, and multiline attributes. IE does not support the multiline attribute. All attributes can be operated using short names, and RegExp. input can be rewritten to RegExp ['$ _'], and so on. RegExp. input is special. It can also be written as RegExp. $ _. The instance attribute of the RegExp object (not useful) is a global Boolean value, indicating whether g has set the ignoreCase Boolean value, and whether I has set the lastIndex integer, it indicates where the next match starts the multiline Boolean value, indicating whether m has set the Source string format of the Source Regular Expression/* use instance attribute */var pattern =/google/ig; alert (pattern. global); // true, whether global, g alert (pattern. ignoreCase); // true, whether to ignore case sensitivity, I alert (pattern. multiline); // false, whether to support line breaks, m alert (pattern. lastIndex); // 0, alert (pattern. source); // google, the source word of the Regular Expression String var pattern =/google/g; var str = 'Google google '; pattern. test (str); // google, match the first alert (pattern. lastIndex); // 6, the starting position of the second match (g required) PS: The above is basically useless. In addition, lastIndex has deviations between IE and other browsers in obtaining the next matching position, mainly in non-global matching. LastIndex also supports manual setting and direct value assignment.