Find string in string javascript

Examples

Search for "Blue":

let text = "Mr. Blue has a blue house";
let position = text.search("Blue");

Try it Yourself »

Search for "blue":

let text = "Mr. Blue has a blue house";
let position = text.search("blue");

Try it Yourself »

Search for /Blue/:

let text = "Mr. Blue has a blue house";
let position = text.search(/Blue/);

Try it Yourself »

Search for /blue/:

let text = "Mr. Blue has a blue house";
let position = text.search(/blue/);

Try it Yourself »

Search case insensitive:

let text = "Mr. Blue has a blue house";
let position = text.search(/blue/i);

Try it Yourself »


Definition and Usage

The search() method matches a string against a regular expression **

The search() method returns the index (position) of the first match.

The search() method returns -1 if no match is found.

The search() method is case sensitive.


Syntax

string.search(searchValue)

Parameters

Parameter Description
searchValue Required.
The search value.
A regular expression (or a string that will be converted to a regular expression).

Return Value

Type Description
A number The position of the first match.
-1 if no match.


The Difference BetweenString search() and String indexOf()

The search() cannot take a start position argument.

The indexOf() method cannot search against a regular expression.


The Difference BetweenString search() and String match()

The search() method returns the position of the first match.

The match() method returns an array of matches.


Regular Expression Search Methods

In JavaScript, a regular expression text search, can be done with different methods.

With a pattern as a regular expression, these are the most common methods:

ExampleDescription
text.match(pattern) The String method match()
text.search(pattern) The String method search()
pattern.exec(text) The RexExp method exec()
pattern.test(text) The RegExp method test()


Browser Support

search() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes


Examples

Search a string for "welcome":

let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome");

Try it Yourself »

Search a string for "Welcome":

let text = "Hello world, welcome to the universe.";
let result = text.indexOf("Welcome");

Try it Yourself »

Find the first occurrence of "e":

let text = "Hello world, welcome to the universe.";
text.indexOf("e");

Try it Yourself »

Find the first occurrence of "e", starting at position 5:

let text = "Hello world, welcome to the universe.";
text.indexOf("e", 5);

Try it Yourself »

Find the first occurrence of "a":

let text = "Hello world, welcome to the universe.";
text.indexOf("a");

Try it Yourself »


Definition and Usage

The indexOf() method returns the position of the first occurrence of a value in a string.

The indexOf() method returns -1 if the value is not found.

The indexOf() method is case sensitive.


Syntax

string.indexOf(searchvalue, start)

Parameters

Parameter Description
searchvalue Required.
The string to search for.
start Optional.
The position to start from (default is 0).

Return Value

Type Description
A number The first position where the search-value occurs.
-1 if it never occurs.


The indexOf() method cannot search against a regular expression.

The search() cannot take a start position argument.



Browser Support

indexOf() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes


How do you check if a string contains another string in JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

How do you find a specific character in a string JavaScript?

The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Can I index a string in JavaScript?

Introduction. A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Each character in a JavaScript string can be accessed by an index number, and all strings have methods and properties available to them.

How do I check if a string contains a substring?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.