substr() === | function matches(string, cursor, seeking) {
return string.substr(cursor, seeking.length) === seeking;
}
matches("abcdef", 3, "de");
matches("abcdef", 3, "defg");
matches("abcdef", 3, "x");
| ready |
check character by character, early exit | function matches(string, cursor, seeking) {
if (cursor + seeking.length > string.length) { return false; }
else {
for (var i = 0; i < seeking.length; i++) {
if (string[cursor+i] !== seeking[i]) { return false; }
}
}
return true;
}
matches("abcdef", 3, "de");
matches("abcdef", 3, "defg");
matches("abcdef", 3, "x");
| ready |
slice() === | function matches(string, cursor, seeking) {
return string.slice(cursor, cursor + seeking.length) === seeking;
}
matches("abcdef", 3, "de");
matches("abcdef", 3, "defg");
matches("abcdef", 3, "x");
| ready |
substring() === | function matches(string, cursor, seeking) {
return string.substring(cursor, cursor + seeking.length) === seeking;
}
matches("abcdef", 3, "de");
matches("abcdef", 3, "defg");
matches("abcdef", 3, "x");
| ready |
regex.test() | function matches(string, cursor, seeking) {
return new RegExp(".{"+cursor+"}"+seeking).test(string);
}
matches("abcdef", 3, "de");
matches("abcdef", 3, "defg");
matches("abcdef", 3, "x");
| ready |