Common JS Regular Expressions

Source: Internet
Author: User

Common JS Regular Expressions
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")); 
Split
String segmentation
var str = "Hello Hunger   , oh I am Hunger"; str.split("");str.split(/\s+/); 
Regular Expression? [Abc] searches for any character between square brackets.
var str="Is this all there is?"; var patt1=/[a-h]/g; console.log(str.match(patt1)); 
? [^ Abc] searches for any characters that are not in square brackets.
var str="hello jikexueyuan!"; var patt1=/[^jike]/g; console.log(str.match(patt1)); 
? [0-9] search for any number from 0 to 9 .? [A-z] search for any characters from lowercase to lowercase .? [A-Z] Find any character from uppercase A to uppercase Z .? [A-z] searches for any character from uppercase A to lowercase z .? [Adgk] searches for any character in a given set .? [^ Adgk] searches for any character outside the given set .? 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)); 
?. Searches for a single character, except for line breaks and line Terminators.
var str="That's hot!"; var patt1=/h.t/g; document.write(str.match(patt1)); 
? \ W search for word characters (letters, numbers, and underscores ).
var str="Give 100%!";  var patt1=/\w/g; document.write(str.match(patt1)); 
? \ W searches for non-word characters.
var str="Give 100%!";  var patt1=/\W/g; document.write(str.match(patt1)); 
? \ D.
var str="Give 100%!";  var patt1=/\d/g; document.write(str.match(patt1)); 
? \ D: searches for non-numeric characters.
var str="Give 100%!";  var patt1=/\D/g; document.write(str.match(patt1)); 
? \ S searches for blank characters (space, tab, line feed, and carriage return ).
var str="Is this all there is?";  var patt1=/\s/g; document.write(str.match(patt1)); 
? \ S.
var str="Is this all there is?";  var patt1=/\S/g; document.write(str.match(patt1)); 
? \ 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)); 
? \ B matches non-word boundary .? \ N.
var str="Hello Hunger.\n be a FE.";  var patt1=/\n/g; document.write(str.search(patt1)); 
? 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)); 
? N * matches any string containing zero or more n.
var str="Hellooo Hunger! Hello World!";  var patt1=/lo*/g; document.write(str.match(patt1)) 
? N? Match any string containing zero or one n.
var str="1, 100 or 1000?";  var patt1=/10?/g; document.write(str.match(patt1)); 
? 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)); 
? N {X, Y} matches the string that contains X or Y n sequences.
var str="100, 1000 or 10000?"; var patt1=/\d{3,4}/g;  document.write(str.match(patt1)); 
? 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)); 
? N $ matches any string ending with n.
var str="Is this his"; var patt1=/is$/g; document.write(str.match(patt1)); 
? ^ N matches any string starting with n.
var str="Is this his"; var patt1=/^Is/g; document.write(str.match(patt1)); 
Common Regular Expressions

? Chinese characters: [\ u4e00-\ u9fa5]
? Mobile phone number: 1 [0-9] {10}
? Email: (\ S) + [@] {1} (\ S) + [.] {1} (\ w) +

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.