Exec, test, match, search, replace, and split usage in js

Source: Internet
Author: User

Exec: Regular Expression is performed on the string, and the matching result is returned. array [0] is the original string, and array [I] is the position that matches the entire string to be searched.
Test: test whether a string contains a matching result. If the string contains a matching result, true is returned. If the string does not contain a matching result, false is returned.
Match (pattern): regular match is performed based on pattern. If it matches, the matching result is returned. If it does not match, null is returned.
Search (pattern): regular match is performed based on pattern. If a result is matched, the number of indexes is returned; otherwise,-1 is returned.
Replace (pattern, replacement): Perform regular match based on pattern and replace the matching result with replacement.
Split (pattern): Regular segmentation based on pattern, returns a split Array
Instance: Copy codeThe Code is 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.exe c (u );
Var a = s. test (u );
Var a = u. match (s );
Var a = u. search (s );
Alert ();
}

Replace

The simplest measure is the ability to replace simple characters. The sample code is as follows:Copy codeThe Code is as follows: var u = "javascript is a good script language ";
// Here I want to replace letter a with Letter
Var s =/a/g;
Var a = u. replace (s, "");
</Script>

SplitCopy codeThe Code is as follows: <script type = "text/javascript">
Var str = "How are you doing today? "
Document. write (str. split ("") + "<br/> ")
Document. write (str. split ("") + "<br/> ")
Document. write (str. split ("", 3 ))
</Script>

Output result:
How, are, you, doing, today?
H, o, w, a, r, e, y, o, u, d, o, I, n, g, t, o, d,, 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.