programming-examples/js/String/Find a word within a string.js

26 lines
675 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
function search_word(text, word){
var x = 0, y=0;
for (i=0;i< text.length;i++)
{
if(text[i] == word[0])
{
for(j=i;j< i+word.length;j++)
{
if(text[j]==word[j-i])
{
y++;
}
if (y==word.length){
x++;
}
}
y=0;
}
}
return "'"+word+"' was found "+x+" times.";
}
console.log(search_word('The quick brown fox', 'fox'));
console.log(search_word('aa, bb, cc, dd, aa', 'aa'));