The difference between the JS regular expression test method, the Exec method and the string search method

Source: Internet
Author: User

1. The regular expression test method

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

return value:

Returns trueif the string contains text that matches Regexpobject, otherwise false.

2. The regular expression Exec method

The exec () method is used to retrieve the matching of regular expressions in a string.

Return value: returns an array that holds the results of the match. If no match is found, the return value is null.

3. String Search method

The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression.

Return value: The starting position of the first substring in stringobject that matches the regexp.

4. Sample code:

<!DOCTYPE HTML><HTMLLang= "zh">    <Head>        <MetaCharSet= "UTF-8" />        <title>Match and replace methods for strings in JavaScript</title>    </Head>    <Body>        <Scripttype= "Text/javascript">            varStr= "Visit W3school w3school!"            //Console Output 6Console.log (Str.search (/W3school/)); varReg= NewRegExp ("W3school"); //Console Output True indicates if the string contains text that matches the regular expressionConsole.log (Reg.test (str)); //Console output: "W3school", Index:6, Input: "Visit w3school w3school!"            //The first "W3school" represents the text that matches the regular expression            //The second index:6 represents the position of the first character of the matched text            //Third input: "Vvisit w3school w3school!" A string that indicates that the retrieved strings are storedConsole.log (reg.exec (str)); </Script>    </Body></HTML>

Note: The exec usage of regular expressions is more complex, the test method of regular expressions and the search method of strings are relatively concise .

The difference between the JS regular expression test method, the Exec method and the string search 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.