0

I want my code to print out all instances of name.

var text = "Eric bla bla bla bla bla Eric bla bla bla Eric";
var myName = "Eric";
var hits = [];

for(var i = 0 ; i<text.length;i++){
    if(text.indexOf(myName) >= 0){
                hits.push(myName);
        }
}

console.log(hits);

Here it prints out like 20 instances of "Eric" in array 'hits'.

How to only print 3 instances(exactly 3 is in string 'text').

joki00
  • 81
  • 3
  • 12

6 Answers6

3

Use String.prototype.match() function to find all instances of search name:

var text = "Eric bla bla bla bla bla Eric bla bla bla Eric",
    myName = "Eric",
    hits = text.match(new RegExp("\\b" + myName +"\\b", "g"));

console.log(hits);

\b - points to a word boundary

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
1

You should split have split before counting. Not the each character.

var arr = text.split(" ");
for(var i = 0 ; i<arr.length;i++){
    if(arr[i]==myName){
                hits.push(myName);
        }
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

Save the index you got last time and use it in subsequent calls to indexOf to skip that occurrence. You also don't want to loop for every character, index indexOf scans; this is a place where do-while is useful:

var text = "Eric bla bla bla bla bla Eric bla bla bla Eric";
var myName = "Eric";
var hits = [];
var index = -1;
do {
  index = text.indexOf(myName, index + 1);
  if (index >= 0) {
    hits.push(myName);
  }
} while (index >= 0);

console.log(hits);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • This works,but its kinda hard for me to understand(still a beginner),answer with array.split its easier for me.Thanks! – joki00 Mar 19 '17 at 10:23
  • 2
    @joki00: Fair enough. Beware that the answer using `split` will fail if Eric is followed by puncutation ("You know, Eric, you're a good bloke."). And the above will identify "Eric" even in the middle of something else ("Hi, Erica"), which could be a downside for the above (or not, depends on what you want). – T.J. Crowder Mar 19 '17 at 10:28
1

You may use a while loop with the position as value for checking. Then use the position as start value for the next look up.

var text = "Eric bla bla bla bla bla Eric bla bla bla Eric",
    myName = "Eric",
    hits = [],
    p = text.indexOf(myName);

while (p !== -1) {
    hits.push(myName);
    p = text.indexOf(myName, p + 1);
}

console.log(hits);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

The code below may can help you.

var text = "Eric bla bla bla bla bla Eric bla bla bla Eric";
var myName = "Eric";
var hits = [];

var lastIndex = text.indexOf(myName);
while (lastIndex !== -1) {
    hits.push(myName);
    lastIndex = text.indexOf(myName, lastIndex + myName.length);
}
console.log(hits);
Y.Peng
  • 400
  • 3
  • 5
0

First of all it print Eric as many character are present in string including white space so, for print ing 3 element statically set limit to 3

 for(var i = 0 ; i<3;i++)

or according to your question there is total 45 character so you can use like this

for(var i = 0 ; i<text.length;i++)
RANVIR GORAI
  • 1,226
  • 11
  • 10
  • *"or printing 3 element statically set limit to 3"* - The point is to count the number of times "Eric" appears in the input string. So no particular limit should be set, and the total number of characters is irrelevant. – nnnnnn Mar 19 '17 at 10:40