Borislav Hadzhiev
Thu Oct 21 2021·3 min read
Photo by Matthew Henry
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
.
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 substringfalse
if it doesn'tstartsWith
method performs case sensitive search, if you want to ignore the case, convert the string and the substring to lowercase before making the comparison.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'); }
startsWith
method is not supported in Internet Explorer. If you need to support the browser, use the next approach covered in this article.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.
// 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.
startsWith
method, but if you have to support Internet Explorer, it gets the job done.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.
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.
// 👇️ 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.