admin管理员组

文章数量:1399956

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 ing 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.

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 ing 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.

Share Improve this question asked Dec 5, 2014 at 6:47 user2112700user2112700 1212 silver badges8 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

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);

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;
    }
}      

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
}

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!");
}

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);

本文标签: arraysSearching a text from a paragraphJavascriptStack Overflow