JavaScript String Search
String Search Methods
- String indexOf()
- String lastIndexOf()
- String search()
- String match()
- String matchAll()
- String includes()
- String startsWith()
- String endsWith()
JavaScript String indexOf()
The indexOf()
function tells you the position of the initial appearance of one string in another string.
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()
function tells you where the final or last appearance of a particular word or phrase is in a piece of text.
Both indexOf() and lastIndexOf() functions will give you -1 if the text is not found.
Both ways allow you to specify a starting position for the search by using a second parameter.
The lastIndexOf()
function search from the end to the start. Imagine this:
If you set the second parameter as 15
, it begins searching at position 15 and moves toward the beginning of the string.
JavaScript String search()
The search()
function looks for a specific word or pattern within a text, and it tells you where it's found.
Did You Notice?
The two techniques, indexOf() and search(), are they the same?
They accept the same arguments (parameters), and return the same value?
The two ways are not the same. Here are the distinctions:
- The
search()
method cannot take a second start position argument.
- The
indexOf()
method cannot take
powerful search values (regular expressions).
In a future section, you'll gain a better understanding of regular expressions.
JavaScript String match()
The match()
function gives you an array with the findings when you compare one string with another string or a pattern.
Note :
Without the g modifier (global search) in a regular expression, match() will only find and return the first match in the string.
Read more about regular expressions in the chapter JS RegExp.
JavaScript String matchAll()
The matchAll()
function gives you a way to find and list all the matches when you compare one string with another string or a regular expression.
If you use a regular expression as a parameter, make sure to set the global flag (denoted by "g"). If you don't do this, it will result in an error of type "TypeError."
If you want to search case insensitive, the insensitive flag (i) must be set:
Notes :
matchAll()
is an ES2020 feature.
matchAll()
does not work in Internet Explorer.
JavaScript String includes()
The includes()
method returns true if a string contains a specified value.
If not, it will give you "false."
Notes :
includes()
is case sensitive.
includes()
is an ES6 feature.
includes()
is not supported in Internet Explorer.
JavaScript String startsWith()
The startsWith()
function tells us if a string starts with a particular value, and it returns true
if it does.
Otherwise it returns false
:
A start position for the search can be specified:
Notes :
startsWith()
is case sensitive.
startsWith()
is an ES6 feature.
startsWith()
is not supported in Internet Explorer.
JavaScript String endsWith()
The endsWith()
function tells us if a string finishes with a particular value, and it gives back true
if it does.
Otherwise it returns false
:
Notes :
endsWith()
is case sensitive.
endsWith()
is an ES6 feature.
endsWith()
is not supported in Internet Explorer.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>The indexOf() function tells you where the first time a word shows up in a sentence.</p>
<p>The position of the first occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The lastIndexOf() Method</h2>
<p>The position of the last occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>Both indexOf() and lastIndexOf() return -1 if the text is not found:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("John");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>The indexOf() function takes another value to begin searching from a specific position.</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate", 15);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The lastIndexOf() Method</h2>
<p>The lastIndexOf() function can start searching from a specific position if you give it a second parameter.</p>
<p>Keep in mind, when you use the lastIndexOf() method, it looks for something starting from the 15th position going backwards until it finds the beginning.</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate", 15);
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The search() Method</h2>
<p>The search method tells you where the first time you see a word in a sentence.</p>
<p>The position of the first occurrence of "locate" is:</p>
<p id="demo"></p>
<script>
let text = "Please locate where 'locate' occurs!";
let index = text.search("locate");
document.getElementById("demo").innerHTML = index;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The match() Method</h2>
<p>Perform a search for "ain":</p>
<p id="demo"></p>
<script>
let text = "The rain in SPAIN stays mainly in the plain";
const myArr = text.match("ain");
document.getElementById("demo").innerHTML = myArr.length + " " + myArr;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll("Cats");
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/g);
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The matchAll() Method</h2>
<p>ES2020 intoduced the string method matchAll().</p>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
const iterator = text.matchAll(/Cats/gi);
document.getElementById("demo").innerHTML = Array.from(iterator);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The includes() Method</h2>
<p>Check if a string includes "world":</p>
<p id="demo"></p>
<p>The includes() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.includes("world");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p>Check if a string starts with "Hello":</p>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("Hello");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The startsWith() Method</h2>
<p id="demo"></p>
<p>The startsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.startsWith("world");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strings</h2>
<p>Check if a string ends with "Doe":</p>
<p id="demo"></p>
<p>The endsWith() method is not supported in Internet Explorer.</p>
<script>
let text = "John Doe";
document.getElementById("demo").innerHTML = text.endsWith("Doe");
</script>
</body>
</html>