854 B
854 B
title, type, shortTitle, language, tags, cover, author, dateModified
| title | type | shortTitle | language | tags | cover | author | dateModified | |
|---|---|---|---|---|---|---|---|---|
| String starts with substring | snippet | Starts with substring | javascript |
|
boutique-home-office-3 | chalarangelo | 2022-07-31T05:00:00-04:00 |
Checks if a given string starts with a substring of another string.
- Use a
for...inloop andString.prototype.slice()to get each substring of the givenword, starting at the beginning. - Use
String.prototype.startsWith()to check the current substring against thetext. - Return the matching substring, if found. Otherwise, return
undefined.
const startsWithSubstring = (text, word) => {
for (let i in word) {
const substr = word.slice(-i - 1);
if (text.startsWith(substr)) return substr;
}
return undefined;
};
startsWithSubstring('/>Lorem ipsum dolor sit amet', '<br />'); // '/>'