Skip to content

Instantly share code, notes, and snippets.

@llagerlof
Last active October 29, 2019 18:15
Show Gist options
  • Save llagerlof/2963d80d27f8cecd757124ad54423559 to your computer and use it in GitHub Desktop.
Save llagerlof/2963d80d27f8cecd757124ad54423559 to your computer and use it in GitHub Desktop.
Return true if string is inside another string, locale independent.
function substringExists(haystack, needle)
{
size_haystack = haystack.length;
size_needle = needle.length;
loop = size_haystack - size_needle;
for (i=0; i < loop + 1; i++) {
sub = haystack.substr(i, size_needle);
c = sub.localeCompare(needle, {}, {sensitivity: 'base'});
if (c === 0) {
return true;
}
}
return false;
}
console.log(substringExists("A Maçã é uma fruta estranha.", "maça"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment