Js Regular Expression (I)

Source: Internet
Author: User

1. Create a regular expression in two ways: create 1 with the new modifier and the number of faces. 1 with the new modifier, create 1 var b2 = new RegExp ('box', 'ig '); // The second parameter is the pattern string: used to limit the matching method. 2 // g: global matching. I: Ignore case-insensitive matching. m: create a parameter string between 1 var d =/Box/I ;; the second/is followed by the pattern string 2. the test method of the Regular Expression in js: test () method: checks whether the string "contains" the corresponding pattern string, returns true and false. exec (): checks whether the string contains a pattern string. The returned value is an array of matched strings, if no value exists, null is returned (Return Value Type: object). Copy code 1 var d =/Box/I; // ignore case-insensitive matching 2 alert (d. test ('box '));/ /True3 4 alert (/Box/I. test ('this is a box'); // true: whether the matching string "contains" matches the string 5 6 alert(d.exe c ('box ')); // box 7 8 alert (typeof d.exe c ('s '); // copy the code object. 3. Use the regular expression 'string' in the String class method to match (), search (), replace (), split () 1, match () string matching method, returns the matching array 1 var pattern =/Box/gi; // global match is case insensitive 2 var B = 'this is a box! It is a small box '; 3 alert (B. match (pattern); // returns the matched array (box, box) 4 // if it is not a global match, it will only match the first one, the returned array contains only one element, 5 pattern =/Box/I; 6 alert (B. match (pattern); // box 2 search () returns the first matched string index position, where g (Global match) does not work, -1 alert (B. search (pattern); // 10 3 replace (): locate the string to be matched, and then replace it with the specified string. Return Value: returns the replaced string 1 alert (B. replace (pattern, 'shit'); // returns the replaced string. Note: if it is not a global match, only the first matched string is replaced. 4. split () according to the specified "match" Split 1 alert (B. split (pattern); // This is ,! It is a small. The static and instance methods of regular expressions are not introduced here. Because they are not commonly used, they are used for query. 4. acquisition and control (* Key Points) 1. match a single character and number. The following Code demonstrates how to copy the Code 1 var pattern =/[a-z] oogle /; // [a-z] indicates any 2 alert (pattern. test ("google"); // true3 4 // note the following (***) 5 var pattern =/[A-Z] oogle /; // If the matching string contains "matched string" 6 alert (pattern. test ('aaaaggggoogleaaa'); // true7 8 alert (/[0-9] aaaa /. test ("444 aaaa"); // The true Copy code can also be "composite" to match 1 var pattern3 =/[a-zA-Z0-9]/; // match both a-z and A-Z and 0-9 can be similar to "/w" 2 3 var pat Tern4 =/[^ a-z] oogle/; // [^ a-z] indicates the matching code of the blank characters other than the-z character 2: 1 // matching of space characters \ s2 var pattern =/goo \ sgle/; 3 var str = 'Goo gl'; 4 5 alert (pattern. test (str); // true 3. The matching of the anchor character mainly demonstrates the matching control code: copy code 1 var pattern = '/^ [0-9] oogle/' // ^ at the beginning of the matched string indicates that the matched string matches 2 alert (pattern. test ("4444 oogle"); // false because it is a 3 4 alert (/^ [0-9] + oogle /. test ("4444 oogle"); // true + indicates one or more 5 6 // Global Control Generate 7 var pattern2 =/^ \ woogle $/; // control at the beginning of the lecture, and $ control at the end, so it is global control ,,, \ w indicates matching numbers, letters, and _ \ B: The end of the line matches 1 var pattern2 =/google \ B/; // \ B indicates whether the end is reached ;;; is whether e is the last character 2 var str2 = 'googleeeeee'; 3 4 alert (pattern2.test (str2); // false5 alert (pattern2.test ('Google ')); // true 5 duplicate matching code Demonstration: Copy code 1 var pattern =/go {3} gle /; // limit o to three 2 var pattern2 =/go {2, 4} gle/; // limit o to two to four 3 var pattern3 =/go {3 ,} gle/; // set o to 3 or more 4 5 var d =' Google '; 6 alert (pattern. test (d); // false7 alert (pattern2.test ('gooog'); // true8 alert (pattern3.test ('goooooggle ')); // demo of replacement matching code in true 6: 1 var pattern =/box | \ w + oogle /; // returns true2 alert (pattern. test ('box'); // true3 alert (pattern. test ('shigggoogle '); // true 7: group matching record characters: Use the static attribute $1 of RegExp to obtain the group content. () Demonstration of group matching: 1 var pattern3 =/google {4, 8} $/; // represents matching e characters for 4 to 8 times 2 var str3 = 'googleeee '; 3 4 alert (pattern3.test (str3); // true5 6 var pattern4 =/(google) {4, 8 }/; // match google 4 to 8 times 7 alert (pattern4.test ('googlegooglegooglegoogle '); // true $1: Obtain the string of the first matched group (difficult ): copy code 1 var pattern =/8 (. *) 8/; 2 var str = 'this is a 8google8'; 3 // It can be matched once; no matter how it matches 4 // pattern. test (str); 5 6 str. match (pattern); // same as above, below You can also obtain the string 7 8 that the group matches. // either of the above two methods must be matched. Then you can obtain the string that the first group matches. 9 alert (RegExp. $1); // google copy the code to demonstrate a case. Print the matched string in bold. 1 var str2 = 'You are a 8shit8'; 2 3 document. write (str2.replace (pattern, '<strong> $1 </strong>'); // $1 extract the matched grouping string to demonstrate the second case, change the location of the word 'Google baidu' to 1 var pattern2 = /(. *) \ s (. *)/; 2 var str = 'Google baidu'; 3 alert (str. replace (pattern2, '$2 $ 1'); // change their positions to 8. Greedy and inert Example 1: copy code 1 var pattern =/[a-z] +/; // greedy Mode 2 var str = 'ddfdfds '; 3 4 alert (str. repl Ace (pattern, 1); // 1 // only matches the first one. Replace the first one with 5 6 pattern =/[a-z]/g; // perform global match 7 8 alert (str. replace (pattern, 1); // 1111111 9 10 // use the inert mode 11 pattern =/[a-z] +? /; 12 alert (str. replace (pattern, 1); // 1dfdfds copy Code The following shows a classic example of greedy Mode 1 // greedy Mode 2 var pattern2 =/8 (. *) 8/g; 3 var str = '8google8 8google8 88 8'; // The First Matching of both sides is 88. also match 84 // <strong> google8 8google8 8 google </strong> 5 document. write (str. replace (pattern2, '<strong> $1 </strong>'); Use the inert mode: Copy code 1 // The following uses the inert Mode 2 pattern2 =/8 (. *?) 8/g; 3 document. write (str. replace (pattern2, "<strong> $1 </strong>"); 4 // output result: 5 // <strong> google </strong> 6 // <strong> google </strong> 7 // <strong> google </strong>

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.