Last active
October 29, 2019 18:15
-
-
Save llagerlof/2963d80d27f8cecd757124ad54423559 to your computer and use it in GitHub Desktop.
Return true if string is inside another string, locale independent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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