Check if String starts with Substring in JavaScript

avatar

Borislav Hadzhiev

Thu Oct 21 20213 min read

banner

Photo by Matthew Henry

Check if String starts with Substring #

To check if a string starts with a substring, call the startsWith() method on the string, passing it the substring as a parameter. The startsWith method returns true if the string starts with the substring, otherwise it returns false.

index.js
const str = 'hello world'; if (str.startsWith('hello')) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string starts with hello'); } else { console.log('โ›”๏ธ string does NOT start with hello'); }

We used the String.startsWith method to determine if a string starts with a specific substring.

The method returns a boolean result:

  • true if the string starts with the substring
  • false if it doesn't
The startsWith method performs case sensitive search, if you want to ignore the case, convert the string and the substring to lowercase before making the comparison.
index.js
const str = 'HELLO world'; const substr = 'hello'; if (str.toLowerCase().startsWith(substr.toLowerCase())) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string starts with hello'); } else { console.log('โ›”๏ธ string does NOT start with hello'); }
The startsWith method is not supported in Internet Explorer. If you need to support the browser, use the next approach covered in this article.

Check if String starts with Substring using indexOf #

To check if a string starts with a substring, call the indexOf() method on the string, passing it the substring as a parameter. If the indexOf method returns 0, then the string starts with the substring, otherwise it doesn't.

index.js
// Supported in IE const str = 'hello world'; const substr = 'hello'; if (str.indexOf('hello') === 0) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string starts with hello'); } else { console.log('โ›”๏ธ string does NOT start with hello'); }

The String.indexOf method returns the index of the first occurrence of a substring in a string.

If the substring is not contained in the string, it returns -1.

If the method returns 0, we can conclude that the string starts with the substring.

This solution is not as direct and readable as using the startsWith method, but if you have to support Internet Explorer, it gets the job done.

Check if String starts with Substring using Regex #

To check if a string starts with a substring, use the test() method with a regular expression that matches the substring in the beginning of the string. The test method will return true if the string starts with the substring and false otherwise.

index.js
if (/^abc/.test('abc123')) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string starts with abc'); } else { console.log('โ›”๏ธ string does NOT start with abc'); }

We used the RegExp.test method to check if a string begins with a specific substring.

The method returns true if the regular expression is matched in the string and false otherwise.

The forward slashes / / mark the start and end of the regular expression.

The caret ^ matches the beginning of the input.

In this example, we check if the string abc123 starts with the substring abc.

If you ever need help reading a regular expression, bookmark this regex cheatsheet from MDN. It's by far the best one out there.

If you want to make the regular expression case insensitive, add the i flag.

index.js
// ๐Ÿ‘‡๏ธ with `i` flag if (/^abc/i.test('ABC123')) { // ๐Ÿ‘‡๏ธ this runs console.log('โœ… string starts with abc'); } else { console.log('โ›”๏ธ string does NOT start with abc'); }

The i flag allows us to perform case insensitive search in the string.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee