A brief analysis of JavaScript string lookup functions: IndexOf and search

Source: Internet
Author: User

Grammar

①indexof: Method returns the position of the first occurrence of a specified string value in a long string. If the lookup string is not found, return-1.

IndexOf

Description: The method will retrieve the string stringobject from beginning to end to see if it contains substring searchvalue. The location at which to begin retrieving is at the fromindex of the string or at the beginning of the string (when no fromindex is specified). If a searchvalue is found, the position of the first occurrence of the Searchvalue is returned. The character position in Stringobject is starting at 0.

Note: the IndexOf () method is case-sensitive!

The ②search: method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression. If not found, returns-1.

Search

Description: The search () method does not perform a global match, and it ignores the flag G. It ignores the LastIndex property of RegExp and always retrieves from the beginning of the string, which means that it always returns the first matching position of the stringobject.

Note: The search () method is case-sensitive!

Application Scenarios

If you are looking for a specific string, you should use IndexOf (), which consumes less system resources and is more efficient;

If you are looking for a string with some characteristics, you need to use a regular expression with the search () method.

More often than not to get the position of a string, indexof is used to determine whether a long string contains substrings.

Web front-end/h5/javascript Learning Group: 250777811 Welcome to my public number, public number name: Web front-end edu. Sweep the following QR code or collection below the QR code attention (Long press the following QR code image, and select the QR code in the identification diagram)

A brief analysis of JavaScript string lookup functions: IndexOf and search

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.