Common JS Regular Expressions: js Regular Expressions

Source: Internet
Author: User

Common JS Regular Expressions: js Regular Expressions

I am on the right track. If you want to learn more about the usage of js regular expressions, please refer to this tutorial.

Definition and use

var patt1 = new RegExp("hello"); var patt2 = /world/ ; 

Test Method

The test () method retrieves the specified value in the string. The return value is true or false.

var pat = /my/; var str = "this is my code..."; console.log(pat.test(str)); // true 

Exec Method

The exec () method retrieves the specified value in the string. The returned value is the value found. If no matching is found, null is returned.

Var pat =/hello/; lele.log(pat.exe c ("oh hello world"); // return hello

Regular Expression type

/Pattern/attributes
The attributes parameter is an optional string. common attributes are "g" and "I", which are used to specify global matching and case-sensitive matching respectively.

Var str = "Visit Hunger"; var patt1 =/hunger/I; // case-insensitive console. log (str. match (patt1); // global match var str = "hello hunger valley! I am hunger "; var patt1 =/hunger/g; console. log (str. match (patt1); // case-insensitive, global match var str = "hello Hunger valley! I am hunger "; var patt1 =/hunger/gi; console. log (str. match (patt1 ));

String Regular Expression

1. search

String search

var str="Visit W3School!"; console.log(str.search(/w3school/)); //-1 console.log(str.serach(/w3school/i)); // 6 

2. match

String Matching

var str="1 plus 2 equal 33"; console.log(str.match(/\d+/)); //[1] console.log(str.match(/\d+/g)); //[1,2,33] 

3. replace

String replacement

var str="Hello JI! oh I am hunger" console.log(str.replace(/Hunger/, "valley")); console.log(str.replace(/hunger/ig, "hunger")); 

4. split

String segmentation

var str = "Hello Hunger , oh I am Hunger"; str.split("");str.split(/\s+/); 

Regular Expression

Between [abc] searches for any characters between square brackets.

var str="Is this all there is?"; var patt1=/[a-h]/g;console.log(str.match(patt1)); 

Character [^ abc] searches for any characters that are not in square brackets.

var str="hello jikexueyuan!"; var patt1=/[^jike]/g; console.log(str.match(patt1)); 

Period [0-9] searches for any number ranging from 0 to 9.

Refer [a-z] to search for any characters from lowercase to lowercase.

Upper [A-Z] looks for any character from upper case A to upper case Z.

Refer [A-z] to find any characters from uppercase A to lowercase z.

 [Adgk] searches for any character in a given set.

Character [^ adgk] searches for any character outside the given set.

Optional red | blue | green searches for any specified options.

var str="hello hunger! How are you?"; var patt1=/hello|you/g; console.log(str.match(patt1)); 

Except for line breaks and line terminator.

var str="That's hot!"; var patt1=/h.t/g; document.write(str.match(patt1)); 

Character \ w to search for word characters (letters, numbers, and underscores ).

var str="Give 100%!"; var patt1=/\w/g; document.write(str.match(patt1));

Optional \ W: searches for non-word characters.

var str="Give 100%!"; var patt1=/\W/g; document.write(str.match(patt1)); 

Numbers \ d.

var str="Give 100%!"; var patt1=/\d/g; document.write(str.match(patt1)); 

Numeric \ D to search for non-numeric characters.

var str="Give 100%!"; var patt1=/\D/g; document.write(str.match(patt1)); 

Blank characters (space, tab, line feed, and carriage return) are searched for in bytes \ s ).

var str="Is this all there is?";var patt1=/\s/g; document.write(str.match(patt1)); 

Bytes \ S to find non-blank characters.

var str="Is this all there is?"; var patt1=/\S/g; document.write(str.match(patt1)); 

Limit \ B matches the word boundary.

// \ Bm/matches 'M' in "moon ';
/Oo \ B/does not match 'oo 'in "moon", because 'n' after 'oo' is a word character;
/Oon \ B/matches 'on' in "moon", because 'on' is at the end of the string, and there are no word characters behind it;

var str="Hello jikexueyuan"; var patt1=/\bjikexueyuan/g; document.write(str.match(patt1)); 

Limit \ B matches non-word boundary.

Linefeed \ n.

var str="Hello Hunger.\n be a FE."; var patt1=/\n/g; document.write(str.search(patt1)); 

Limit n + matches any string containing at least one n.

var str="Hello HHunger! Hello World!"; var patt1=/H+/g; document.write(str.match(patt1)); var str="Hello Hunger! Hello World!"; var patt1=/\w+/g; document.write(str.match(patt1)); 

Limit n * matches any string containing zero or multiple n.

var str="Hellooo Hunger! Hello World!"; var patt1=/lo*/g; document.write(str.match(patt1)) 

Limit n? Match any string containing zero or one n.

var str="1, 100 or 1000?"; var patt1=/10?/g; document.write(str.match(patt1)); 

Limit n {X} matches the string that contains X n sequences.

var str="100, 1000 or 10000?"; var patt1=/\d{4}/g; document.write(str.match(patt1)); 

Substring n {X, Y} matches the string that contains the X or Y n sequences.

var str="100, 1000 or 10000?"; var patt1=/\d{3,4}/g; document.write(str.match(patt1)); 

Substring n {X,} matches strings that contain at least X n sequences.

var str="100, 1000 or 10000?"; var patt1=/\d{3,}/g; document.write(str.match(patt1)); 

Substring n $ matches any string ending with n.

var str="Is this his"; var patt1=/is$/g; document.write(str.match(patt1)); 

Substring ^ n matches any string starting with n.

var str="Is this his"; var patt1=/^Is/g; document.write(str.match(patt1)); 

Common Regular Expressions

Invalid Chinese Character: [\ u4e00-\ u9fa5]
Vivo mobile phone number: 1 [0-9] {10}
Mailbox: (\ S) + [@] {1} (\ S) + [.] {1} (\ w) +

Articles you may be interested in:
  • Explanation of Regular Expression usage in JScript
  • Javascript Regular Expression usage Summary
  • Use of the replace function in js Regular Expressions
  • Introduction to JS Regular Expressions
  • Examples of common Regular Expressions and usage in PHP and javascript
  • Usage of question marks in js Regular Expressions
  • Example of using a javascript Regular Expression in search ()
  • Example of js Regular Expressions test () and exec ()
  • Basic JS Regular Expression usage (Classic full)
  • Example of Regular Expression usage in JSP

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.