The @[toc] regular is used to define some rules for strings. The program can determine whether a string conforms to the rules according to these rules, and can also extract the content of a string that conforms to the rules.

Create a regular expression:

var reg = new RegExp   ("Regular"."Matching pattern");
var reg = / Regular expression /Match the patternCopy the code

grammar

Matching mode: You can set no matching mode, one matching mode, or all matching modes. The setting sequence is not required

  • I: Ignores case
  • G: global matching mode

Regular syntax:

  • |
  • []
  • (^)In addition to
  • [a-z] Lowercase letters
  • [A-Z]The capital letters
  • [A-z]Any letter
  • [0-9] Any Numbers

Example 1

			var str="Hello World!";
			var reg1=/[abc]/;		//false whether to include a or B or C
			var reg2=/a|d|e/;		// True is a, d, or e
			var reg3=/[^e]/;         // True: specifies whether to include e
			var reg4=/ [^ Hello World!] /; // if false contains Hello World! The accident
			var reg5=/ [0-9];		//false Specifies whether the value contains digits ranging from 0 to 9
Copy the code

Example 2

			var str="hello world!";
			var reg1=/[A-z]/;		//true
			var reg2=/[a-z]/;		//true
			var reg3=/[A-Z]/;		//false
			var reg4=/[A-Z]/i;		//true
Copy the code

Example 3

			var str="abc adc aec";
			var reg1=/a[gmn]c/;    //false
				/ / a (GMN) c is equivalent to agc | amc | anc
			var reg2=/a[bde]c/;		//true
			var reg3=/a[bfg]c/;		//true
Copy the code

quantifiers

  • N {X} matches a string containing X sequences of n. For example,

    • /a{2}/ does not match “a” in “candy,”
    • /a{2}/ matches both a’s in “caandy,” and the first two a’s in “caaandy.”
    • Abb/ab {2} / matching
    • / matching (ab) {2} / abab
  • N {X,} X is a positive integer. The previous pattern n matches when it occurs at least X times in a row. For example,

    • /a{2,}/ does not match “a” in “candy”,
    • /a{2,}/ matches all the “a” in “caandy” and “caaaaaaandy.”
  • N {X,Y} X and Y are positive integers. The preceding pattern N matches when it occurs at least X and at most Y times in a row. For example,

    • /a{1,3}/ not matching “cndy”
    • /a{1,3}/ match the “a” in “candy,” “caandy,”
    • /a{1,3}/ matches the first three “a” s in “caaaaaaandy”. Note that when “caaaaaaandy” is matched, the match is “aaa” even if the original string has more “A”.
  • N + matches any string that contains at least one n. It can be understood as n{1,} for example

    • /a+/ match “a” in “candy”,
    • /a+/ matches all the “a” in “caaaaaaandy”.
  • N * matches any string containing zero or more n’s. For example,

    • /bo*/ match “boooo” in “A ghost Booooed “,
    • /bo*/ match “b” in “A bird warbled”
    • /bo*/ = “A goat grunted”
  • n? Matches any string containing zero or one n. For example,

			var str = "angel" ;
			var reg = /x? / ;	//true
			var reg = /a? / ;	//true
			var reg = /(aa)? / ;	//true
Copy the code
  • n$ Matches any string ending in n.
  • ^nMatches any string that starts with n.
  • ? =nMatch any followingImmediately followingA string that specifies string n.
			var str="Is this all there is";
			var reg = /is(? = all)/;	//true
			var reg = /is(? = a)/ ;	//true
			var reg = /is(? = l)/ ;	//false
			var reg = /is(? = I)/ ;	//false
Copy the code
  • ? ! nMatches any string that is not immediately followed by the specified string n.

metacharacters

metacharacters describe note
. Finds single characters, except newlines and line terminators Used in regular expressions\As an escape word

\. said. \ \said\
\w Find word characters The equivalent of[A-z0-9_]Contains any alphanumeric underscore
\W Find non-word characters The equivalent of[^A-z0-9_]Except for digits, letters and underscores
\d Find the number The equivalent of[0-9]
\D Find non-numeric characters The equivalent of[^ 0-9]
\s Find whitespace characters Contain Spaces
\S Find non-whitespace characters Does not contain Spaces
\b Match word boundaries
\B Matches non-word boundaries
\ 0 Finding NULL characters
\n Look for newline characters
\f Find the page feed character
\r Find carriage return
\t Look for tabs
\v Find the vertical TAB
\xxx Finds characters specified in the octal number XXX
\xdd Finds characters specified as a hexadecimal number dd
\uxxxx Finds a Unicode character specified as a hexadecimal number XXXX
Example 1: Removing Spaces at different locations
“`js
var str = " hello world " ; console.log(str.replace(/\s/g,"")); / / output "helloworld" console. The log (STR. Replace (/ * ^ \ s/g, "")); / / output "hello world" the console. The log (STR) replace (* $/ \ s/g, "")); / / output "hello world" the console. The log (STR) replace (/ ^ | \ \ s * * $s/g, "")); // Output "Hello world"Copy the code
Example 2: Email match js // any alphanumeric underline in the message format. Any alphanumeric underline @ any alphanumeric. Any letter (2-5) var reg = / ^ \ w {3} @ [A - z0-9] + (\. [a-z] {2, 5})} {1, 2 $/; var email = "[email protected]" ; //true var email = "[email protected]" ; //true var email = "[email protected]" ; //true var email = "[email protected]" ; //true var email = "abc0asfabc.com.cn" ; //false var result=reg.test(email); console.log(result);Copy the code

Parse: Email format: any alphanumeric digits underscore (at least 3 characters) @ Any alphanumeric digits. Any letters (2-5). Any letter (2-5) regular expression: ^ \ w {3} (. \ \ w +) * @ [A – z0-9] + (\. [a-z] {2, 5})} {1, 2 $

  • Begin with any alphanumeric digit underscore →^\w{3,}
  • @ –@
  • Any alphanumeric →[A-z0-9]
  • Ends in. Any letter and contains 1 or 2 letters(\ [a-z] {2, 5})} {1, 2 $Such as:qq.com edu.com.cnMaybe one or two in the back.xxx

methods

  • Test Retrieves the specified value in the string. Returns true or false.
  • Exec retrieves the value specified in the string. Returns the value found and determines its location.
  • Compile was deprecated in version 1.5. Compile regular expressions.

Associated with the string method

  • Search () searches for the specified content parameter in the original string based on the regular expression or substring: the regular expression or string will query the content based on the expression and return the index of the first match, or -1 if no match is found.
			var str="Hello World! Hi Sian! Hello CSDN";
			var result=str.search("!");	/ / 11
			var result=str.search("。");	/ / 1
			var result=str.search(/h/);	//-1 has no lowercase h
			var result=str.search(/h/i);	//0 I Ignores case
			var result=str.search(/h/ig);	//0 I Ignores case
Copy the code
  • Split () can split one according to the specified contentstringorRegular expressionsSplit into an array
    • Parameter: a string, as an argument, will split the array according to the string. You can receive a regular expression, which will split the array according to the regular expression
			var str="Hi Sian! Hello csdn";
			var result=str.split("s");  // Divide by lowercase s
			// Output (2) ["Hi Sian! Hello c", "dn"]
			var result=str.split("");  // Empty string partition
			/ / output (19) [" H ", "I", ""," S "and" I ", "a", "n", "! ", ""," H ", "e", "l", "l", "o", ""," c ", "S", "d", "n"]
			var result=str.split(/s/i);  // Partition by s, case insensitive
			// Output (3) ["Hi ", "Ian! Hello c", "dn"]
Copy the code
  • Match () can extract the content of the string that matches the regular expression as an argument: the regular expression can be used to extract the content of the string that matches the regular expression and return it as an array.If the argument is not a RegExp object, you need to first pass it to the RegExp constructor to convert it to a RegExp object.The results of
    • If no matching substring is found, -1 is returned (some browsers may output null).
    • If we find the results
      • If the regular expression matches globally, the corresponding array is returned
      • If the regular expression is not a global match, the index of the first occurrence is returned
			var str = "The rain in SPAIN stays mainly in the plain";
			var result = str.match(/ain/);   // Global index is not enabled
			/ / output 5
			var result = str.match(/ain/g);		// Global index
			Output (3) ["ain", "ain", "ain"]
			var result = str.match(/[0-9]/g);		
			/ / output is null
Copy the code
  • Replace () replaces the specified content in the string with the new content parameter
    • The first: The content to be replaced, either a regular expression or a string. If it is a string, only the first content retrieved from the beginning will be replaced.
    • Second: Replace the new content, the string
			var str = "The rain in SPAIN stays mainly in the plain";
			var result = str.replace("ain"."Meow");
			// The r cat in SPAIN stays mainly in The plain
			var result = str.replace(/ain/."Meow");
			// The r cat in SPAIN stays mainly in The plain
			var result = str.replace(/ain/g."Meow");	// Global match
			// The r meow in SPAIN stays m meow ly in The pl meow
			var result = str.replace(/ain/ig."Meow");	// Ignore case globally
			// The r cat in SP cat stays m cat ly in The pl cat
Copy the code