BEST SITE FOR WEB DEVELOPERS

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Let JS Const JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Events JS Strings JS String Methods JS String Search JS String Templates JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop For In JS Loop For Of JS Loop While JS Break JS Iterables JS Sets JS Maps JS Typeof JS Type Conversion JS Bitwise JS RegExp JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Arrow Function JS Classes JS JSON JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words

JS Versions

JS Versions JS 2009 (ES5) JS 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / Edge JS History

JS Objects

Object Definitions Object Properties Object Methods Object Display Object Accessors Object Constructors Object Prototypes Object Iterables Object Sets Object Maps Object Reference

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Bind Function Closures

JS Classes

Class Intro Class Inheritance Class Static

JS Async

JS Callbacks JS Asynchronous JS Promises JS Async/Await

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM Forms DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

Web API Intro Web Forms API Web History API Web Storage API Web Worker API Web Fetch API Web Geolocation API

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery Selectors jQuery HTML jQuery CSS jQuery DOM

JS Graphics

JS Graphics JS Canvas JS Plotly JS Chart.js JS Google Chart JS D3.js

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Certificate

JS References

JavaScript Objects HTML DOM Objects

Place for your advertisement!
JavaScript. W3Schools in English. Lessons for beginners

Ua

JavaScript String Search

JavaScript Search Methods

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

JavaScript String indexOf()

The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:

Example

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate");
Try it Yourself »

Note

JavaScript counts positions from zero.

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


JavaScript String lastIndexOf()

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

Example

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate");
Try it Yourself »

Both indexOf(), and lastIndexOf() return -1 if the text is not found:

Example

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("John");
Try it Yourself »

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

Example

let str = "Please locate where 'locate' occurs!";
str.indexOf("locate", 15);
Try it Yourself »

The lastIndexOf() methods searches backwards (from the end to the beginning), meaning: if the second parameter is 15, the search starts at position 15, and searches to the beginning of the string.

Example

let str = "Please locate where 'locate' occurs!";
str.lastIndexOf("locate", 15);
Try it Yourself »

JavaScript String search()

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

Example

let str = "Please locate where 'locate' occurs!";
str.search("locate");
Try it Yourself »

Did You Notice?

The two methods, indexOf() and search(), are equal?

They accept the same arguments (parameters), and return the same value?

The two methods are NOT equal. These are the differences:

  • The search() method cannot take a second start position argument.
  • The indexOf() method cannot take powerful search values (regular expressions).

You will learn more about regular expressions in a later chapter.


JavaScript String match()

The match() method searches a string for a match against a regular expression, and returns the matches, as an Array object.

Example 1

Search a string for "ain":

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/g);
Try it Yourself »

Read more about regular expressions in the chapter JS RegExp.

Note

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

Syntax

string.match(regexp)
regexp Required. The value to search for, as a regular expression.
Returns: An Array, containing the matches, one item for each match, or null if no match is found

Example 2

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

let text = "The rain in SPAIN stays mainly in the plain";
text.match(/ain/gi);
Try it Yourself »

JavaScript String includes()

The includes() method returns true if a string contains a specified value.

Example

let text = "Hello world, welcome to the universe.";
text.includes("world");
Try it Yourself »

Syntax

string.includes(searchvalue, start)
searchvalue Required. The string to search for
start Optional. Default 0. Position to start the search
Returns: Returns true if the string contains the value, otherwise false
JS Version: ES6 (2015)

Check if a string includes "world", starting the search at position 12:

let text = "Hello world, welcome to the universe.";
text.includes("world", 12);
Try it Yourself »

Browser Support

includes() is an ES6 feature (JavaScript 2015).

It is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

includes() is not supported in Internet Explorer.


JavaScript String startsWith()

The startsWith() method returns true if a string begins with a specified value, otherwise false:

Example

let text = "Hello world, welcome to the universe.";

text.startsWith("Hello");
Try it Yourself »

Syntax

string.startsWith(searchvalue, start)

Parameter Values

Parameter Description
searchvalue Required. The value to search for.
start Optional. Default 0. The position to start the search.

Examples

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

The startsWith() method is case-sensitive.


Browser Support

startsWith() is an ES6 feature (JavaScript 2015).

It is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

startsWith() is not supported in Internet Explorer.


JavaScript String endsWith()

The endsWith() method returns true if a string ends with a specified value, otherwise false:

Example

Check if a string ends with "Doe":

let text = "John Doe";
text.endsWith("Doe");
Try it Yourself »

Syntax

string.endsWith(searchvalue, length)

Parameter Values

Parameter Description
searchvalue Required. The value to search for.
length Optional. The length to search.

Check if the 11 first characters of a string ends with "world":

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

Try it Yourself »

Note

The endsWith() method is case-sensitive.


Browser Support

endsWith() is an ES6 feature (JavaScript 2015).

It is supported in all modern browsers:

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

endsWith() is not supported in Internet Explorer.


Complete String Reference

For a complete String reference, go to our:

Complete JavaScript String Reference.

The reference contains descriptions and examples of all string properties and methods.


Place for your advertisement!