Javascript --- regular expression (1)

Source: Internet
Author: User

Javascript --- regular expression (1)

The regular expression is an object that describes the character pattern. it is used to verify whether the input data of the client complies with the established rules, such as limiting the length or format of input characters. because of client verification, it can save a lot of system resources on the server side and provide a better user experience.

① Create a regular expression

Two creation methods:

Var box = newRegExp ('box', 'ig '); // The first parameter can be an optional string, and the second parameter pattern modifier var box =/box/ig; // directly use two backslash

② Test the Regular Expression

// Test the pattern matching in the string. true or falsevar pattern =/box/I; var str = 'box'; alert (pattern. test (str); // use exec to return the matching array var pattern =/box/I; var str = 'box'{alert(pattern.exe c (str ));

③ Use the regular expression of a string

/* Use the match method to obtain the matching array */var pattern =/box/ig; // global search var str = 'this is abox! That is a box too! '; Alert (str. match (pattern); // box, box, match to two boxalert (str. match (pattern ). length); // 2, get the array length/* use search to find matching data */var pattern =/Box/ig; // globally case-insensitive search. note that the search method returns the result, that is, g is dispensable. var str = 'this is abox! That is a box too! '; Alert (str. search (pattern); // 10, locate the returned position; otherwise, return-1/* replace the matched data with replace */var pattern =/box/ig; var str = 'this is abox! That is a box too! '; Alert (str. replace (pattern, 'Tom'); // replace box with tom


Running result:

/* Use split to split the string array */var pattern = // ig; var str = 'this is abox! That is a box too! '; Alert (str. split (pattern); // splits spaces into arrays.

Running result:

/* Use static attributes */var pattern =/(g) oogle/; var str = 'this isgoogle! '; Pattern. test (str); alert (RegExp. input); // thisis google! Alert (RegExp. leftContext); // thisisalert (RegExp. rightContext );//! Alert (RegExp. lastMatch); // googlealert (RegExp. lastParen); // galert (RegExp. multiline); // false, Which is undefined in IE

Authorization + acrHt/HS0cno1sNsYXN0bG5kZXjV + 8r9o6y0 + rHtz8K0zsalxeS9q7TTxMTA79fWt/authorization = ">

/* Use instance attributes */var pattern =/google/ig; alert (pattern. global); // true, whether alert (pattern. ignoreCase); // true, whether to ignore case sensitive alert (pattern. multiline); // false, whether line feed alert (pattern. lastIndex); // 0, alert (pattern. source); // google, the source string of the Regular Expression

/* Use the dot metacharacters */varpattern =/g... gle /;//... match three arbitrary characters var str = 'gooogle'; alert (pattern. test (str); // true/* use the character class to match */var pattern =/g [a-zA-Z] * gle /; // [a-z] * indicates any character in a-z, Which is case insensitive, you can try to remove the changes in * [] to see the effect var str = 'Google '; alert (pattern. test (str); // true varpattern =/g [^ 0-9] * gle/; // [^ 0-9] * indicates any non-0 ~ 9 character var str = 'go0gle '; alert (pattern. test (str); // false var pattern =/[a-z] [A-Z]/; // [a-z] or [A-Z] any number of characters, var str = 'Google '; alert (pattern. test (str); // true/* use the metacharacter to match */var pattern =/g \ w * gle /; // \ w * matches any number of all letters and numbers and _ var str = 'Goo _ gl'; alert (pattern. test (str); // true var pattern =/\ D {7,}/; // \ D {7 ,} match at least 7 Non-numeric var str = 'J J'; alert (pattern. test (str); // true

Var pattern =/goo \ sgle/; // \ s can match the space var str = 'Goo gl'; alert (pattern. test (str); // true var pattern =/google \ B/; // \ B can match whether the boundary var str = 'googlel'; alert (pattern. test (str); // false

/* Use the anchor character to match */var pattern =/^ google $/; // ^ match from the beginning, $ match var str = 'Google 'from the end '; alert (pattern. test (str); // true

/* Duplicate match */var pattern =/g * gle/; // match 0 ~ N var str = 'Google ';//*,?, +, {N, m} alert (pattern. test (str); // true

/* Use or pattern matching */var pattern =/google | baidu | bing/; // google | baidu | one of the three bing strings var str = 'Google '; alert (pattern. test (str); // true


Related Article

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.