
JavaScript String Search and Pattern Matching
Finding and Extracting Text with Precision
Catalog
Click on this table of contents to jump to the corresponding section
JavaScript String indexOf()
The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it returns -1 if the string is not found:
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:
Both indexOf(), and lastIndexOf() return -1 if the text is not found:
Both methods accept a second parameter as the starting position for the search:
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.
JavaScript String search()
The search() method searches a string for a string (or a regular expression) and returns the position of the match:
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 returns an array containing the results of matching a string against a string (or a regular expression).
Perform a search for "ain":
Perform a search for "ain":
Perform a global search for "ain":
Perform a global, case-insensitive search for "ain":
If a regular expression does not include the g modifier (global search), match() will return only the first match in the string. Read more about regular expressions in the chapter JS RegExp.
JavaScript String matchAll()
The matchAll() method returns an iterator containing the results of matching a string against a string (or a regular expression).
If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.
If you want to search case insensitive, the insensitive flag (i) must be set:
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. Otherwise it returns false.
Check if a string includes "world":
Check if a string includes "world". Start at position 12:
includes() is case sensitive. includes() is an ES6 feature. includes() is not supported in Internet Explorer.
JavaScript String startsWith()
The startsWith() method returns true if a string begins with a specified value. Otherwise it returns false:
- Returns true:
- Returns false:
A start position for the search can be specified:
- Returns false:
- Returns true:
startsWith() is case sensitive.
startsWith() is an ES6 feature.
startsWith() is not supported in Internet Explorer.
JavaScript String endsWith()
The endsWith() method returns true if a string ends with a specified value. Otherwise it returns false:
Check if a string ends with "Doe":
Check if the 11 first characters of a string ends with "world":
endsWith() is case sensitive.
endsWith() is an ES6 feature.
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.
Lily and 4 people like this
Prev Post
Space The Final Frontier
Next Post
Telescopes 101
0 Comments
Leave a Reply
Recent Post

SMOTE for Imbalanced Classification with Python
12:28:00 16/05/2026

CLAHE Histogram Equalization with OpenCV: A Python Guide
12:06:00 16/05/2026

Histogram Equalization in Digital Image Processing
10:56:00 16/05/2026

Gaussian Noise Explained: What It Is and How It Works
10:36:00 16/05/2026

Python Image Blurring with OpenCV: Gaussian, Median & Bilateral Filter Guide
09:59:00 16/05/2026
Related Topics
Feeds
Don't miss what's next 👋
Enjoyed this content? Leave your email to get notified when we publish new insights, tutorials, and updates — no spam, ever.

