JS exec, test, match, search, replace, split usage _ Regular expression

Source: Internet
Author: User
EXEC: Regular processing of string and return of matching result. Array[0] is the original string, Array[i] to match the position in the entire searched string.
Test: Tests whether string contains a matching result, contains return true, and does not contain return false.
Match (pattern): a regular match based on pattern and, if matched to, returns a matching result, such as a match that does not return null
Search (Pattern): a regular match based on pattern and, if matched to a result, returns the number of its indexes; otherwise, return-1
Replace (pattern,replacement): a regular match based on pattern, which replaces the matching result with a replacement
Split (pattern): Regular segmentation based on pattern, returning an array of partitions
Instance:
Copy Code code as follows:

function Checkform () {
var u = document.form_name.check.value;
var s =/^[-a-za-z0-9_]+ (\.[ -a-za-z0-9_]+) *@[-a-za-z0-9_]+ (\.[ -a-za-z0-9_]+) *$/;
var a = s.exec (u);
var a = s.test (u);
var a = U.match (s);
var a = U.search (s);
alert (a);
}


Replace

The simplest kind of ability is the simple character substitution. The sample code is as follows:
Copy Code code as follows:

var U = "JavaScript is a good script language";
Here I want to replace the letter A with the letter A
var s =/a/g;
var a = U.replace (S, "a");
</script>

Split
Copy Code code as follows:

<script type= "Text/javascript" >
var str= "How are your doing today?"
document.write (Str.split ("") + "<br/>")
document.write (Str.split ("") + "<br/>")
document.write (Str.split ("", 3))
</script>

The output results are:
How,are,you,doing,today?
H,o,w, A,r,e, Y,o,u, D,o,i,n,g, T,o,d,a,y,?
How,are,you

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.