Take JavaScript notes (5)-Regular Expression Basics

Source: Internet
Author: User
JavaScript Regular & mdash; RegExp: RegExp has a test () method. If a given string (with only one parameter) matches this mode, true is returned. Otherwise, false is returned. Differences and meanings of/I,/g,/ig,/gi,/m in Regular Expressions/I (case-insensitive)/g (the result of full-text search... 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, fd, safd, safd, and ds
[Javascript]
/**
* Split ()
*/
Alert (str. split ("@"));
// Use a regular expression to implement the same function
Alert (str. split (/@/));
[Html]

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]
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.