Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript String matchAll()

The matchAll() method provides an iterator that holds the outcomes of matching a string against another string or a regular expression.

Example

const iterator = text.matchAll(“Cats”);

If the parameter is a regular expression, it is necessary to set the global flag (g); otherwise, a TypeError will be thrown.

Example

const iterator = text.matchAll(/Cats/g);

To conduct a case-insensitive search, ensure the insensitive flag (i) is set.

Example

const iterator = text.matchAll(/Cats/gi);

Note

matchAll() is a feature introduced in ES2020.

However, it does not function in Internet Explorer.

JavaScript String includes()

The includes() method returns true if a specified value is found within a string; otherwise, it returns false.

Examples

Verify whether a string contains “world”.

let text = “Hello world, welcome to the universe.”;
text.includes(“world”);

Check if a string includes “world”, beginning from position 12.

let text = “Hello world, welcome to the universe.”;
text.includes(“world”12);

Note

includes() is case-sensitive.

includes() is a feature introduced in ES6.

includes() lacks support in Internet Explorer.

JavaScript String startsWith()

The startsWith() method returns true if a string commences with a specified value; otherwise, it returns false.

Examples

Returns true:

let text = “Hello world, welcome to the universe.”;
text.startsWith(“Hello”);

Returns false:

let text = “Hello world, welcome to the universe.”;
text.startsWith(“world”)

You can specify a starting position for the search.

Returns false:

let text = “Hello world, welcome to the universe.”;
text.startsWith(“world”5)

Returns true:

let text = “Hello world, welcome to the universe.”;
text.startsWith(“world”6)

Note

startsWith() is sensitive to case.

startsWith() is a feature introduced in ES6.

startsWith() is not compatible with Internet Explorer.

JavaScript String endsWith()

The endsWith() method returns true if a string concludes with a specified value.

otherwise, it returns false.

Example

Verify whether a string concludes with “Doe”.

let text = “John Doe”;
text.endsWith(“Doe”);

Verify whether the first 11 characters of a string conclude with “world”.

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

Note

endsWith() is sensitive to case.

endsWith() is a feature introduced in ES6.

endsWith() is not compatible with Internet Explorer.