JavaScript Learning Notes (14) Regular Expression _ basics

Source: Internet
Author: User
Tags character classes control characters processing instruction

RegExp class
The RegExp object's constructor can take one or two arguments
The first parameter describes the pattern string that needs to be matched, and if there is a second argument, this parameter makes an extra processing instruction.
first, the basis
1.1 Using the RegExp object
Test () method
Whether the Test matches. If the given string (with only one argument) matches this pattern, it returns true, otherwise it returns false

Copy Code code as follows:

var stomatch = "Cat";
var recat =/cat/; Regular expression literal using Perl-style syntax
Alert (Recat.test (Stomatch)); Outs "true"

exec () method
There is a string argument that returns an array. The first entry in the array is the first match, and the other is the reverse reference. (That is, there is only one in the array and the first match)
Copy Code code as follows:

var straaa = "A bat, a cat, a fat bat, a fat cat";
var regat = new RegExp ("at", "GI");
var arr = regat.exec (STRAAA); Arr[0] is "at" and the Arr.index value is the 3,arr.lastindex value of 5
Match () method

Returns an array of all the matches contained in the string.
var straaa = "A bat, a cat, a fat bat, a fat cat";
var regat = new RegExp ("at", "GI");
var arrmatch = Straaa.match (Regat); Note: string. Match (parameters are matching characters) contrary to the above
Search () method
Similar to indexof (), returns a matching position that appears in the string. Its argument is a RegExp object rather than just a substring.
Copy Code code as follows:

var straaa = "A bat, a cat, a fat bat, a fat cat";
var regat = new RegExp ("at", "GI");
var index = Straaa.search (Regat); Outputs "3" first occurrence position is 3

1.2 Extended String method
Replace () method
You can replace the first argument with the second argument, where the first argument can also be a regular expression.
var strbbb = "The Sky is red.";
Replace all s In the above sentence with a regular expression to find all the matching
var strnewbbb = Strbbb.replace (/s/gi, "# #"); Replace All "s" (regardless of case) with # #
To upgrade again, the second parameter can also be a function
Copy Code code as follows:

var stochange = "The sky is red.";
var rered =/red/;
var sresulttext = stochange.replace (rered, function (Smatch) {
Return "Blue";
});
alert (Sresulttext);

In this example, the value of the Smatch in the function is always "red" (since this is the only matching pattern). The first occurrence of red "is replaced by the return value" blue "of the function.
Additional:
For this sentence in the book "Because this is the only matching pattern" I think it should mean that, replace only two parameters, the first parameter found is unique, the function of the parameter smatch should be the first parameter of the value, the only matching mode ...
Split () method
Copy Code code as follows:

var scolor = "Red,blue,yellow,green";
var recomma =/\,/;
var arrcolors = Scolor.split (Recomma); Split at each comma
alert (arrcolors.length); Outputs "4"

The regular expression Recomma must have a backslash before the comma because the comma has a special meaning in the syntax and must be escaped.
Second, Simple mode
2.1-Dollar character
All the metacharacters used by a regular expression are:
( [ { \ ^ $ | ) ? * + .
Altogether 12. You need to escape when you use these meta characters at any time, which is to precede with a backslash.
Cases:
var reqmark =/\?/; Escape
var reqmark=new RegExp ("\?"); Notice here that the double escape, because itself the backslash also needs to escape
So we should try to use the first case, the literal syntax! The style of Perl
2.2 Using special characters
In addition, there are some other predefined special characters, as listed in the following table:
Character description
----------------------------------------------------
\ t tab
\ n Line Feed
\ r return character
\f Page Breaks
\a Alert character
\e Escape character
\CX control characters corresponding to X
\b Fallback character
\v Vertical Tab
Null characters
----------------------------------------------------
2.3 Character Classes
Putting some characters in square brackets makes it very effective to tell regular expressions to match the first, second, third, and so on.
① Character class----Simple class
var stomatch = "A bat,a cat,a fat bat,a fat Cat";
Matches a regular expression with bat or cat or fat
var rebatcatfat =/[bcf]at/gi;
var rebatcatrat=/[\u0062cf]at/gi; Use Unicode form
var arrmatches = Stomatch.match (Rebatcatrat);
Alert (Arrmatches.join (",")); Output "Bat,cat,fat,bat,fat,cat"
② character class----negative class
var stomatch = "A bat,a cat,a fat bat,a fat Cat";
Matches a regular expression with at end, but not beginning with B or C
var rebatcatrat =/[^bc]at/gi; Off-character ^ means no match followed characters
var arrmatches = Stomatch.match (Rebatcatrat);
Alert (Arrmatches.join (",")); Output "Fat,fat"
③ Character class----range class
Specify the range from A to Z: [A-z]. This is case-sensitive.
var stomatch = "Num1,num2,num3,num4,num5,num6,num7,num8,num9";
var reonetofour =/num[1-4]/gi; From 1 to 4
var arrmatches = Stomatch.match (Reonetofour);
Alert (Arrmatches.join (",")); Output "NUM1,NUM2,NUM3,NUM4"
④ character class----combination class
A combination class (combination Class) is a character class that is composed of several other classes.
If you want to match all the letters from A-M and numbers from 1-4, and a newline character, then the class used should be this:
[a-m1-4\n]
Notice that there are no spaces between the inner classes.
⑤ character class----predefined classes
Code is equivalent to matching
----------------------------------------------------------------
. [^\n\r] Any character other than a newline and carriage return
\d [0-9] Number
\d [^0-9] non-numeric characters
\s [\t\n\x0b\f\r] whitespace characters
\s [^ \t\n\x0b\f\r] non-white-space character
\w [a-za-z_0-9] Word characters (all characters, numbers, and underscores)
\w [^a-za-z_0-9] non-word characters
-----------------------------------------------------------------
Using predefined characters makes it obvious that pattern matching becomes easier. For example, a false idea matches 3 digits:
var stomatch = "567 9838 abc";
var rethreenums =/[0-9][0-9][0-9]/;
var rethreenums=/\d\d\d/; More concise with predefined
Alert (Rethreenums.test (Stomatch)); Output "true"
2.4 Classifier
Quantifiers (quantifier) can specify the number of occurrences of a particular pattern. When you specify the number of times a pattern should appear, you can specify a hard number, or you can specify a soft quantity.
1. Simple classifier
--------------------------------------------------------------------
Code description
--------------------------------------------------------------------
? appear 0 or one times
* occurs 0 or more times (any time)
+ occurs one or more times (at least once)
{n} must appear n times
{n,m} appears at least n times but not more than m times
{N,} appears at least n times
--------------------------------------------------------------------
For example, a false idea matches the word bread,read or red. Using the question mark Quantifier, you can match the three by using just one expression:
var rebreadreadorred =/b?rea?d/;
or var rebreadreadorred =/b{0,1}rea{0,1}d/;
2. Greedy, inert and dominant quantifiers
The greedy quantifier first looks at whether the entire string matches. If no match is found, it removes the last character from the string and tries again. If no match is found, the last character is removed again, and the process repeats until a match is found or there is no character left in the string.
The lazy classifier first looks at whether the first letter in the string matches. If this character alone is not enough, enroll in the next character, forming a two character string. If no match is found, the inert classifier continues to add characters from the string until a match is found or the entire string is checked and there is no match. Inert quantifiers and greedy quantifiers work in the opposite way.
The dominant quantifier only attempts to match the entire string. If the entire string does not produce a match, no further attempts are made. The dominant quantifier in fact simply, is across.
--------------------------------------------------------------------
Greedy Inert domination description
--------------------------------------------------------------------
? ?? ? + 0 times or one occurrence
* *? *+ 0 or more times
+ +? + + occurs one or more times
{n} {n}? {n}+ occurs exactly n times
{N,m} {n,m}? {n,m}+ at least n times up to M times
{N,} {N,}? {n,}+ appears at least n times
--------------------------------------------------------------------
Look at the following examples to better understand the above three classifiers
var str = "abbbaabbbaaabbb1234";
var reg1 =/.*bbb/g;
var reg2 =/.*?bbb/g;
var reg3 =/.*+bbb/g; Error in Visual Studio2008 ...
var arrMatches1 = Str.match (REG1);
var arrMatches2 = Str.match (REG2);
var arrMatches3 = Str.match (REG3);
Alert ("Greedy:" + arrmatches1.join (",") + "\ n Inert:" + arrmatches2.join (","));
The main is to match a process differently!

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.