1. What is a regular expression Regular Expression (regularexpression) is an object that describes the character pattern. The RegExp class of ECMAScript represents a regular expression, while both String and RegExp define a strong pattern matching and text retrieval with a regular expression. 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. 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. two creation methods: var reg = new RegExp ('box'); // The first parameter string var reg = new RegExp ('box', 'ig '); // optional mode modifier for the second parameter 2. 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 falseexec in the string to perform a matching search, returns the result array 3. use the regular expression method of the String to use the regular expression method of the String object to include the pattern to return the substring in pattern or replacement to replace patternsearch (pattern) with replacement) returns the start position of 'pattern' in the string) returns the static attributes of the array RegExp object split by the specified pattern. The short name contains the semantic input $ _ the currently matched string lastMatch $ & the last matched string lastParen $ + the last circle matching substring leftContext $ 'in parentheses multi before the last match Line $ * is used to specify whether all expressions are used for the Boolean value rightContext $ 'of multiple rows. The substring Opera after the last match does not support the input, lastMatch, lastParen, and multiline attributes. IE does not support the multiline attribute. The instance attribute of the RegExp object contains the global Boolean value, which indicates 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 Source Regular Expression Source string Form 3. get control regular expression metacharacters are special characters. They have some special functions to control the matching mode. The metacharacter after the backslash will lose its special meaning. Character class: Single Character and number :. match any character except linefeed [a-z0-9] match any character in character set in parentheses [^ a-z0-9] match any character in character set not in parentheses \ d match number \ D match non-digit, the same as [^ 0-9]. \ w matches letters and numbers and _ \ W matches non-letters and numbers and _ ---------------------------------------------- character classes: blank character \ 0 match null character \ B match space character \ f match paper character \ n match line break \ r match carriage return character \ t match Tab character \ s match blank character, space, tab and line breaks \ S match non-blank characters ------------------------------------------------ character classes: anchor character ^ match at the beginning of the line $ match at the end of the line \ A only matches the word boundary at the start of the string \ B match, invalid word in [] \ B match non-word boundary \ G match start position of current search \ Z match character String end or line end \ z only matches string end ---------------------------------------------------- character class: Repeated character x? Match 0 or 1 xx * match 0 or any number of xx + match at least one x (xyz) + match at least one (xyz) x {m, n} matches at least m and at most n x ------------------------------------------------ character class: alternative character this | where | logo matches this or where or any character class in the logo: Record character (string) the group \ 1 or $1 used for reverse reference matches the content in the first group \ 2 or $2 matches the content in the second group \ 3 or $3 matches the content in the third group 4. common Regular Expressions 1. check the zip code var pattern =/[1-9] [0-9] {5}/; // there are 6 digits in total. the first digit cannot be 0var str = '123 '; alert (pattern. test (str); 2. check the file compressed package var pattern =/[\ w] + \. zip | rar | gz/; // \ d \ w _ indicates that all numbers and letters are added with var str = '123.zip ';//\. indicates matching ., next, select alert (pattern. test (str); 3. remove unnecessary spaces var pattern =/\ s/g; // g must be global in order to all match var str = '2017 111 123'; var result = str. replace (pattern, ''); // matches a space into a non-space alert (result); 4. delete spaces at the beginning and end var pattern =/^ \ s +/; // force the first var str = 'Goo gl'; var result = str. replace (pattern, ''); pattern =/\ s + $/; // force end result = result. replace (pattern, ''); alert ('|' + result + '| ');
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.