Skip to content

Instantly share code, notes, and snippets.

@edegula
Created June 25, 2013 15:56
Show Gist options
  • Save edegula/5859647 to your computer and use it in GitHub Desktop.
Save edegula/5859647 to your computer and use it in GitHub Desktop.
Manually searching for texts in JS
var text = "thisis is sisi sis iskallakksdj lakdjf asdf hark \
the cannons hear them Elias alksdfjalksdjkjaskdjflk kajsdfkj \
Erwin kajsdklfjasdfjlkj asdf Erwin sakaksl askdfjaskdfj Erwin",
myName = "Erwin",
hits = [];
for ( var i = 0; i < text.length; i++ ) {
if ( text[i] === myName[0] ) {
var count = 0;
var index = hits.length - 1;
for ( var j = i, k = 0; j < myName.length + i; j++, k++ ) {
if ( text[j] === myName[k]) {
hits.push(text[j]);
count++;
} else {
hits.splice(index,count);
}
}
}
}
if ( hits.length === 0 ) {
console.log("Your name wasn't found!");
} else {
console.log(hits);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment