user2112700
user2112700

Reputation: 121

Searching a text from a paragraph - Javascript?

I have written the below code to search my name from a paragraph,

 /*jshint multistr:true */

text = "Blah blah blah blah blah blah Elic \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits = [];
var l=0;
var count=0;


// Look for "E" in the text
for(var i = 0; i < text.length; i++) {
    if (text[i] === "E") {

        for(var j = i; j < (myName.length + i); j++) {
           for(var k =1; k < 4; k++) {
            if(text[i+k] === myName[k]){

            hits.push(text[j]); 
            }
                else {
                    break;
                }

                }           
            }

        }
    }



if (hits.length === 0) {
    console.log("Your name wasn't found!");
} else {
    console.log(hits);
}

But the output is coming like [ 'E', 'E', 'E', 'r', 'r', 'r', 'i', 'i', 'i', 'c', 'c', 'c', 'E', 'E', 'E', 'r', 'r', 'r', 'i', 'i', 'i', 'c', 'c', 'c', 'E', 'E', 'E', 'r', 'r', 'r', 'i', 'i', 'i', 'c', 'c', 'c' ]

I am sure this is because of the 3rd for loop, Please give some suggestion to fine tune this code so that the output will be like,

[ 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c', 'E', 'r', 'i', 'c' ]

Thanks in advance.

Upvotes: 3

Views: 3305

Answers (5)

Oleksandr T.
Oleksandr T.

Reputation: 77482

1

var res = text.match(new RegExp(myName, 'g')).join('').split('');

2

var text = "Blah blah blah blah blah blah Elic \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

var myName = "Eric";
var hits   = [];
var l      = 0;
var count  = 0;
var foundName = '';  

for (var i = 0; i < text.length; i++) {
  foundName = '';

  if (text[i] === "E") {
    for (var j = 0; j < myName.length; j++) {
      foundName += text[i + j];                
    }

    if (foundName === myName) {
      for (var k = 0; k < myName.length; k++) {
        hits.push(foundName[k]);                
      }
    }
  }
}

console.log(hits);

Upvotes: 4

KaustubhSV
KaustubhSV

Reputation: 358

Please try this ..

     text = "Blah blah blah blah blah blah Elic lah blah blah Eric blah blah Eric blah blah blah blah blah blah blah Eric";

    var charFound=0;
    var myName = ['E' , 'r', 'i' ,'c'];
    var hits = [];
    var l=0;
    for(var i = 0; i < text.length; i++) 
    {
        if(text[i] === myName[charFound])
        {   
          charFound++;
        }
        else
        {
          continue;
          charFound=0;
        }
        if(charFound == 4)
        {
          for(var j = 0; j < 4;  j++)
          {
             hits.push(text[i-j]); 
            }
            charFound = 0;
        }
   }
   // Rearranging the hits counter to match the requirement
   for(var k = 0; k <hits.length/2; k++ )
   {
        var temp="";
        temp = hits[k];
        hits[k]= hits[hits.length-k-1];
        hits[hits.length-k-1] = temp;
   }
  alert(hits);

Upvotes: 0

Allen
Allen

Reputation: 175

You don't need both of those internal for loops. What's happening when you break, you're breaking out of the innermost for loop but are still running the second for loop.

Try this instead of the two internal for loops: (replace the for loop that involves j)

for (var k = 0; k < 4; k++) {
    if (text[i + k] === myName[k]) {
        hits.push(text[i + k]); 
    } else {
        break;
    }
}      

Upvotes: 2

benscabbia
benscabbia

Reputation: 18072

Not sure what you want to do with the result but assuming you just want to test if the name is in a string the you use the .indexOf() method so:

var n = text.indexOf("Eric");

Then the result of n will display the first characters index position and if fails to find the matching word then it will return -1.

So then you add this to a simple test like:

if(text.indexOf("Eric") <0){
     \\doStuff
}

Upvotes: 1

Nishit Maheta
Nishit Maheta

Reputation: 6031

you can try using javascript .indexOf() function.

text = "Blah blah blah blah blah blah Elic \
blah blah blah Eric blah blah Eric blah blah \
blah blah blah blah blah Eric";

if(text.indexOf('Eric') < 0 ){
    console.log("Your name wasn't found!");
}

Upvotes: 0

Related Questions