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