Valerie M.
—You want to check whether a string contains a substring in JavaScript. What are the different ways to do this?
There are a number of ways you could approach this problem. We’ll take a look at two methods: includes()
and indexOf()
.
includes()
methodYou can use JavaScript’s includes()
method to check whether a string contains a substring. This will return true
if the substring is found, or false
if not.
Consider the code example below:
const str = 'This is my example string!'; const substr = 'my'; console.log(str.includes(substr));
The above code would output true
.
Note that includes()
performs a case-sensitive search. To work around this, you can convert the string to lower case using toLowerCase()
as follows:
console.log(str.toLowerCase().includes(substr));
Furthermore, a position
can also be passed as an argument to specify the position at which to begin searching the string for the substring. The default position is 0
. If you want to begin searching at position 2
, you would write:
console.log(str.includes(substr, 2));
indexOf()
methodThe indexOf()
method is also case sensitive, and returns the index position of the first occurrence of the substring within the string. If the substring is not found, it returns -1
.
const str = 'This is my example string!'; const substr = 'my'; console.log(str.indexOf(substr));
The code above returns 8
.
You can also include the position
at which to begin searching the string for the substring. The default position is 0
.
Other search methods you can consider include:
search(regExp)
– Returns the index of the first match between regExp
and the string. If no match is found, -1
is returned.lastIndexOf(searchValue)
– Returns the index position of the last occurrence of searchValue
inside a string.match(searchValue)
– Finds the searchValue
, a regular expression, within a given string and returns an array containing the matches.charAt(index)
– Returns a string representing the character at the specified index. If the index is out of range, it returns an empty string.startsWith(searchValue)
– Returns true
or false
if searchValue
is found at the beginning of a string.endsWith(searchValue)
– Returns true
or false
if a string ends with the searchValue
.If you’re looking to get a deeper understanding of how JavaScript application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.