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').

3
  • 2
    The loop you're using here executes for each character in string. Commented Mar 19, 2017 at 10:16
  • (text.match(new RegExp("\\b" + myName + "\\b", "g")) || []).length Commented Mar 19, 2017 at 11:26
  • here is your answer stackoverflow.com/a/7924240/938822 Commented Feb 11, 2019 at 16:19

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

2 Comments

This works,but its kinda hard for me to understand(still a beginner),answer with array.split its easier for me.Thanks!
@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).
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);

Comments

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

Comments

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

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.