JavaScript String Search

JavaScript method used to search strings:

  • String.indexOf()
  • String.lastIndexOf()
  • String.startsWith()
  • String.endsWith()

String.indexOf()

indexOf() The method returns the specified text in the stringFirstThe index of the occurrence (of the position):

Example

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate")    // Returns 7

Try It Yourself

JavaScript counts positions from zero.

0 is the first position in a string, 1 is the second, 2 is the third, and so on...

String.lastIndexOf()

lastIndexOf() The method returns the index of the last occurrence of the specified text in the string:

Example

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate")    // Returns 21

Try It Yourself

If the text is not found,indexOf() And lastIndexOf() Both return -1:

Example

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("Bill")    // Returns -1

Try It Yourself

Both methods accept the second parameter as the starting position for the search:

Example

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15)    // Returns 21

Try It Yourself

lastIndexOf() The method searches forward (from the end to the beginning), which means that if the second parameter is 15If the second parameter is specified, the search starts from position 15 and continues to the beginning of the string.

Example

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15)    // Returns 7

Try It Yourself

String.search()

search() The method searches for the specified value in the string and returns the position of the match:

Example

let str = "Please locate where 'locate' occurs!";
str.search("locate")     // Returns 7

Try It Yourself

Did you notice?

indexOf() And search() Are these two methods equal?

Do they accept the same parameters and return the same values?

These two methods are not equal. The differences are as follows:

  • search() The method cannot accept a second starting position parameter.
  • indexOf() The method cannot use powerful search values (regular expressions).

You will learn more about regular expressions in the following chapters.

String.match()

The match() method searches for matches in a string based on the regular expression and returns them as an Array object.

Example 1

Search for "ain" in a string:

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g)    // Returns the array [ain,ain,ain]

Try It Yourself

Please learn more about regular expressions in the JS RegExp chapter.

If the regular expression does not contain the g modifier (perform global search), the match() method will only return the first match in the string.

Syntax

string.match(regexp)
regexp Required. The value to be searched, which is a regular expression.
Return: An array containing the matches, each match corresponds to an item, and if no match is found, it is null.

Example 2

Perform a case-insensitive global search for "ain":

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi)   // Returns the array [ain, AIN, ain, ain]}

Try It Yourself

String.includes()

If the string contains the specified value,includes() The method returns true.

Example

let text = "Hello world, welcome to the universe.";
text.includes("world")    // Returns true

Try It Yourself

Browser support

Internet Explorer does not support String.includes().

Chrome IE Firefox Safari Opera
Chrome 41 Edge 12 Firefox 40 Safari 9 Opera 28
March 2015 July 2015 August 2015 October 2015 March 2015

Syntax

string.includes(searchvalue, start)
searchvalue Mandatory. The string to be searched.
start Optional. Default is 0. The starting position for the search.
Return: If the string contains the value, then return true, otherwise return false.
JS version: ES6 (2015)

Check if the string contains "world", starting from position 12:

let text = "Hello world, welcome to the universe.";
text.includes("world", 12)    // Returns false

Try It Yourself

String.startsWith()

If the string starts with the specified value, then startsWith() The method returns true, otherwise return false:

Example

let text = "Hello world, welcome to the universe.";
text.startsWith("Hello")   // Returns true

Try It Yourself

Syntax

string.startsWith(searchvalue, start)

Parameter value

Parameter Description
searchvalue Mandatory. The value to be searched.
start Optional. Default is 0. The starting position for the search.

Example

let text = "Hello world, welcome to the universe.";
text.startsWith("world")    // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 5)    // Returns false
let text = "Hello world, welcome to the universe.";
text.startsWith("world", 6)    // Returns true

Try It Yourself

Note:startsWith() Method is case-sensitive.

Internet Explorer does not support startsWith() Method.

The first fully supported browser version is:

Chrome IE Firefox Safari Opera
Chrome 41 Edge 12 Firefox 17 Safari 9 Opera 28
March 2015 July 2015 August 2015 October 2015 March 2015

String.endsWith()

If the string ends with the specified value, then endsWith() The method returns true, otherwise return false:

Example

Check if the string ends with "Gates":

var text = "Bill Gates";
text.endsWith("Gates")    // Returns true

Try It Yourself

Syntax

string.endsWith(searchvalue, length)

Parameter value

Parameter Description
searchvalue Mandatory. The value to be searched.
length Optional. The length to search for.

Retrieve the first 11 characters of the string ending with "world":

let text = "Hello world, welcome to the universe.";
text.endsWith("world", 11)    // Returns true

Try It Yourself

Note:endsWith() Method is case-sensitive.

Internet Explorer does not support endsWith() Method.

The first browser version to fully support this method is:

Chrome IE Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 April 2017 June 2017 September 2016 June 2016

Complete String Reference Manual

For a complete reference, please visit our full JavaScript String Reference Manual.

This manual includes descriptions and examples of all string properties and methods.