0

I have a string "Hello World". I want the positions of the char "l" from the String.

My code is the following:

str = "Hello World";
pos = str.search(/l/g);
out.value = pos;

The result of this code is 2, but the wanted result is 2,3,9.

How can i get this result?

Edit: thanks for your help.

But now i want to get the sum of (2+1*105) + (3+1*105) + (9+1*105).

Can you help me again?

renokl2014
  • 121
  • 1
  • 11

7 Answers7

4

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

Finding successive matches

If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property).

var str = "Hello World";
var re = /l/g;
var matches = [];

while(match=re.exec(str)) matches.push(match.index);

document.write(matches);
dting
  • 38,604
  • 10
  • 95
  • 114
1

What about a small function to do it?

str = "Hello World";
find = (function(str,c){
    results = []  
    for(i=0;i<str.length;i++){
       if (str[i].toLowerCase()===c)
           results.push(i)
    }
    return results
});
find(str,'l')

Here the working fiddle: http://jsfiddle.net/bx8sj0gv/

nowhere
  • 1,558
  • 1
  • 12
  • 31
  • Thanks for that. And now i want to get the sum of (2+1*105) + (3+1*105) + (9+1*105). How can i do this? Can you help me again? – renokl2014 Aug 03 '15 at 07:26
  • Just replace the line where you have the push function with: results.push(i+105). (i'm not sure why you are adding 1 since it won't change the result. if you want to add 1 to the position and then multiply, you should do results.push((i+1)*105) – nowhere Aug 03 '15 at 11:57
0

The position is two, because there is no loop here, so your search will only hit once, which will only display "2" in this case.

You will need to create and array of chars and loop through it like this:

    input_1 = [ "hello", "world" ];

    for(var i=0; i<input_1.length; i++){
       pos = str.search(/l/g);
       out.value = pos;
    }

This is merely an example, but it will help you understand the concept of it all.

Barr J
  • 10,636
  • 1
  • 28
  • 46
0

Try this:

var str="Hello World";
var needle='l';
var temp=[];
for(var i=0; i < str.lengt
    if(str[i]===haystack){
        temp.push(i)
    }
}
console.log(temp);
Rayon
  • 36,219
  • 4
  • 49
  • 76
0

Your code finds only the first instance of the letter and then returns. You need to list over every character of the string like so:

str = "Hello World"

for (var i = 0, len = str.length; i < len; i++) { if(str[i] == "l") { console.log(i ); } }

JD Angerhofer
  • 148
  • 1
  • 13
0

Here is a link which done this Finding all indexes of a specified character within a string

var str = "scissors";
var indices = [];
for(var i=0; i<str.length;i++) {
if (str[i] === "s") indices.push(i);
}
Community
  • 1
  • 1
0

While loop solution:

var indices = function(find, where) {
  var arr = [];
  for (var i = 0, l = where.length; l > i; i++)
    if (find === where[i])
      arr.push(i);
  return arr;
};

alert(indices("l", "Hello World")); // [2, 3, 9]

Recursive solution:

var indices = function(find, where) {
  var i = where.lastIndexOf(find);
  if (-1 === i) return [];
  return indices(find, where.substr(0, i)).concat([i]);
};

alert(indices("l", "Hello World")); // [2, 3, 9]