Take JavaScript notes (5)-Regular Expression Basics

Source: Internet
Author: User

JavaScript Regular -- RegExp:
RegExp has a test () method. If a given string (with only one parameter) matches this mode, it returns true. Otherwise, it returns false.
Differences and meanings of/I,/g,/ig,/gi,/m in Regular Expressions
/I (case-insensitive)
/G (full-text search for all matching characters)
/M (multi-row search)
/Gi (full-text search, case-insensitive)
/Ig (full-text search, case-insensitive)
[Javascript]
Var str = "jfkd @ s, laj @ fd, safd @ saf, ds @";
Var res = // @/gi; // full-text search @
Alert (res. test (str); // output: true
The exec () method has a string parameter and returns an array. The first entry of the array is the first match, and the rest is the reverse reference.
[Javascript]
// AStr only contains the first instance, which is @ in "jfkd @ sla @
Var aStr = res.exe c (str );
Alert (aStr );
The String object has a match () method, which returns an array containing all matching strings.
[Javascript]
Alert (str. match (res); // output :@,@,@,@
The behavior of another string method called seartch () is similar to that of indexOf (), but it uses a RegExp object rather than just a substring. The Search () method returns a matched position in the string.
[Javascript]
// It starts from the position of the Character index [0]
Alert (str. search (res); // output: 4
Replace ():
[Javascript]
/**
* Replace (), which can replace all the matches of a substring (the first parameter) with another string (the second parameter)
*/
Var sRep = str. replace ("@", "slim horse ");
Alert (sRep); // output: jfkd slim horse s, laj @ fd, safd @ saf, ds @

/**
* You can also use a regular expression as the first parameter.
*/
Alert (str. replace (res, "slim horse"); // output: jfkd slim horse s, laj slim horse fd, safd slim horse saf, ds slim horse


/**
* You can also specify a function as the second parameter of replace.
*/
Var sFun = str. replace (res, function (){
Return "Skinny horse ";
});
Alert ("---------------" + sFun); // output: jfkd, laj, and safd, ds <span> <span class = "comment"> </span>
[Javascript]
/**
* Split ()
*/
Alert (str. split ("@"));
// Use a regular expression to implement the same function
Alert (str. split (/@/));
[Html]
<! -- Example in which a replace method can only be used to process numbers -->
<Input size = "25" value = "" id = "maxnum" name = "tbmeetroom. maxnum" _ cke_saved_name = "tbmeetroom. maxnum"
Onkeyup = "this. value = this. value. replace (/\ D/g ,'')"
Onafterpaste = "this. value = this. value. replace (/\ D/g,'') "/>
All metacharacters in the expression must be escaped (followed by a backslash) to match correctly. Metacharacters are part of the regular expression syntax. Below are all metacharacters used by the regular expression: ([{\ ^ $ | )? * +. Therefore, if you want to match a question mark, then: var reTest = /\? /; Or, var reTest = new RegExp ("\\? "); The second line uses two backslash, called double escape.
Character class:
[Javascript]
<Script type = "text/javascript">
// Simple class -- match "bat", "cat", and "fat"
Var sToMatch = "a bat, a Cat, a fAt baT, a faT cat ";
Var reBatCatRat =/[bcf] at/gi;
Var arrMatches = sToMatch. match (reBatCatRat );
// Alert (arrMatches );

/**
* Negative class: Specifies the characters to be excluded.
* For example, to match all characters except a and B, the character class is [^ AB].
* The escape sign (^) indicates that the regular expression character cannot match the character that follows.
* In the preceding example, if you only want to obtain a word that contains at but cannot start with B or c:
*/
ReBatCatRat =/[^ bc] at/gi;
ArrMatches = sToMatch. match (reBatCatRat );
// Alert (arrMatches );

/**
* Range class-match all the characters in the alphabet, but do not want to input them one by one,
* Available: [a-z]. The hyphen (-) in it indicates "from what to what" (a to z)
* Here, a-z only matches lowercase letters, unless I is used to indicate case insensitive
* If You Want To match only uppercase letters, use: [A-Z]
* Obtains the characters starting with 1, 2, 3, and 4 and starting with number.
*/
SToMatch = "number1, number2, number3, number4, number5, number6, number7, number8, number9 ";
ReBatCatRat =/number [1-4]/gi; // or use negative class/number [^ 5-9]/gi
ArrMatches = sToMatch. match (reBatCatRat );
// Alert (arrMatches );

/**
* Combination class: A character class composed of other classes.
* Assume that you want to match all ~ M letters, numbers from 1 to 4, and a line break
* [A-m1-4 \ n]
* JavaScript/ECMAScript does not support union classes and cross classes in some other regular expressions. This means you cannot have a similar
* [A-m [p-z] or [a-m [^ B-e]
*/


/**
* Pre-defined class
Code is equivalent to matching
. [^ \ N \ r] any character except line feed and carriage return
\ D [0-9] Number
\ D [^ 0-9] non-numeric characters
\ S [\ t \ n \ x0B \ f \ r] blank characters
\ S [^ \ t \ n \ x0B \ f \ r] non-blank characters
\ W [a-zA-Z_0-9] Word characters (all letters, all numbers and underscores)
\ W [^ a-zA-Z_0-9] non-word characters

To match three numbers
*/
SToMatch = "325 fds2 ";
ReBatCatRat =/\ d/; // or use negative class/number [^ 5-9]/gi
Alert (reBatCatRat. test (sToMatch ));
</Script>

From Dan's column

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.