startsWith, endsWith, and Finding Strings
startsWith() and endsWith() return true or false depending on whether a string begins or ends with some given text. includes() does the same for text anywhere in the string. All three are case-sensitive and take plain text. For a pattern, such as “any phone number”, the string methods that take a regular expression do the job instead.
startsWith and endsWith
Section titled “startsWith and endsWith”Each method takes an optional second argument. For startsWith() it is the position where the check begins. For endsWith() it is treated as the length of the string, so the check ends there.
const url = 'https://example.com/shop/basket';
console.log(url.startsWith('https://'));console.log(url.endsWith('/basket'));console.log(url.startsWith('shop', 20));console.log(url.endsWith('example.com', 19));This prints:
truetruetruetrueThese read more clearly than the older idiom of comparing the result of indexOf() with 0.
Ignoring case, and includes
Section titled “Ignoring case, and includes”To compare without regard to case, lower-case the string first. includes() checks for text anywhere, and its optional second argument is the position to start looking from.
const fileName = 'Holiday-Photo.JPG';
console.log(fileName.endsWith('.jpg'));console.log(fileName.toLowerCase().endsWith('.jpg'));console.log(fileName.includes('Photo'));console.log(fileName.includes('photo'));console.log(fileName.includes('Photo', 9));This prints:
falsetruetruefalsefalseWhen the position of the match is needed, rather than a yes or no, use indexOf and lastIndexOf.
Regular expressions are not accepted
Section titled “Regular expressions are not accepted”Passing a regular expression to startsWith(), endsWith() or includes() throws an error, rather than quietly converting it to text.
try { 'report.pdf'.endsWith(/\.pdf$/);} catch (error) { console.log(`${error.name}: ${error.message}`);}This prints:
TypeError: First argument to String.prototype.endsWith must not be a regular expressionSearching for a pattern
Section titled “Searching for a pattern”A regular expression describes a pattern rather than fixed text. Its test() method answers whether the pattern matches. The string method search() returns the position of the first match, or -1, and match() with the g flag returns an array of every match, or null when there is none.
const note = 'Call 0161 496 0000 or 0113 496 0999';
console.log(/\d/.test(note));console.log(note.search(/\d/));console.log(note.match(/\d{4} \d{3} \d{4}/g));console.log(note.match(/\d{5}/g));This prints:
true5[ '0161 496 0000', '0113 496 0999' ]nullBecause match() can return null, code that reads its length needs a fallback, such as (note.match(pattern) ?? []).length.
matchAll() returns each match with its position. It requires the g flag.
const note = 'Call 0161 496 0000 or 0113 496 0999';
for (const match of note.matchAll(/\d{4} \d{3} \d{4}/g)) { console.log(match[0], 'at', match.index);}This prints:
0161 496 0000 at 50113 496 0999 at 22A pitfall with test() and the g flag
Section titled “A pitfall with test() and the g flag”A regular expression with the g flag remembers where its last match ended, in its lastIndex property, and the next test() carries on from there. Reusing one such expression for repeated tests gives answers that change from call to call.
const pattern = /o/g;const word = 'foo';
console.log(pattern.test(word), pattern.lastIndex);console.log(pattern.test(word), pattern.lastIndex);console.log(pattern.test(word), pattern.lastIndex);This prints:
true 2true 3false 0Leave the g flag off an expression used only with test(). Regular expressions have a page of their own in introduction to regex.
Steve Griffith covers this topic in his video startsWith, endsWith, and Finding Strings.