JavaScript Test () method

Source: Internet
Author: User

Definition and Usage:

The test () method is used to detect whether a string matches a pattern.

Grammar:

Regexpobject.test (string);

Regexpobject: Regular expression;

String: Must be a parameter, the character to be detected;

return value:

If string: string contains text that matches the regular expression: Regexpobject, returns True, otherwise false;

Description

Call the test () method of the RegExp object R and pass the string s to it, which is equivalent to this expression: (R.exec (s)! = null).

Usage development:

1 //each regular expression has a LastIndex property that records where the last match ended, including the Exec method. 2 3 varRe =/^[1-9]\d{4,10}$/gi;4 varstr = "123456";5Alert (Re.test (str));//returns True6str = "1234567";7Alert (Re.test (str));//returns false8 9 //Workaround:Ten  One varRe =/^[1-9]\d{4,10}$/gi; A varstr = "123456"; -Alert (Re.test (str));//returns True -  the //after executing the above test, we can eject the -alert (Re.lastindex);//pop up 6 - //That means that the last time after the 6th character ends - //then the next time you call test, the search will continue after the 6th character. +  - //workaround, set the LastIndex property of the regular expression to 0 +  A //The specific code is as follows: at<script type= "Text/javascript" > - varRe =/^[1-9]\d{4,10}$/gi; - varstr = "123456"; -Alert (Re.test (str));//returns True -str = "1234567"; -Re.lastindex=0; inAlert (Re.test (str));//returns True -</script> to  + //or: -  the //Regular mode to remove the global mode, the G is removed on the line, G is to continue down the meaning, not necessarily do not add g, change to: var re =/^[1-9]\d{4,10}$/i; * varstr = "123456"; $Alert (Re.test (str));//returns TruePanax Notoginsengstr = "1234567"; -Alert (Re.test (str));//returns True
Code

JavaScript Test () method

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.