JS to determine if a string contains a string (reproduced)

Source: Internet
Author: User

From:https://www.cnblogs.com/ooo0/p/7741651.html

Method of the String object

Method One: IndexOf () (recommended)

var str = "123"; Console.log (Str.indexof ("3")! =-1);  True

The IndexOf () method returns the position of the first occurrence of a specified string value in a string. If the string value that you want to retrieve does not appear, the method returns-1.

Method Two: Search ()

var str = "123"; Console.log (Str.search ("3")! =-1);  True

The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression. Returns 1 if no matching substring is found.

Method three: Match ()

var str = "123"; var reg = RegExp (/3/); if (Str.match (reg)) {    //include        }

The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.

RegExp Object Methods

Method four: Test ()

var str = "123"; var reg = RegExp (/3/); Console.log (Reg.test (str)); True

The test () method is used to retrieve the value specified in the string. Returns TRUE or FALSE.

Method five: Exec ()

var str = "123"; var reg = RegExp (/3/); if (Reg.exec (str)) {    //contains        }

The exec () method is used to retrieve the matching of regular expressions in a string. Returns an array that holds the results of the match. If no match is found, the return value is null.

JS to determine if a string contains a string (reproduced)

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.