Alex
Alex

Reputation: 1055

search() string for multiple occurrences

Say you have the string, Black cat jack black cat jack black cat jack.

How would you use search() to find the 2nd occurence of the word jack?

I'm guessing the code would look something like:

var str = "Black cat jack black cat jack black cat jack";
var jack = str.search('jack');

But that will only return the location of the first occurrence of jack in the string.

Upvotes: 2

Views: 4762

Answers (3)

Roger Villas
Roger Villas

Reputation: 1

I've figured out this solution -to call the function that searches and replaces the original string recursively, until no more occurrences of the word are found:

function ReplaceUnicodeChars(myString) {    
    var pos = myString.search("&#");

    if (pos != -1) {
        // alert("Found unicode char in string " + myString + ", position " + pos);
        unicodeChars = myString.substr(pos, 6);
        decimalChars = unicodeChars.substr(2, 3);
        myString = myString.replace(unicodeChars, String.fromCharCode(decimalChars));
     }

    if (myString.search("&#") != -1)
        // Keep calling the function until there are no more unicode chars
        myString = ReplaceUnicodeChars(myString);

    return myString;       
}

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56809

Usage recommendation

Note that String.search method works with RegExp - if you supply a string then it will implicitly convert it into a RegExp. It more or less has the same purpose as RegExp.test, where you only want to know whether there is a match to the RegExp in the string.

If you want to search for fixed string, then I recommend that you stick with String.indexOf. If you really want to work with pattern, then you should use RegExp.exec instead to get the indices of all the matches.

String.indexOf

If you are searching for a fixed string, then you can supply the position to resume searching to String.indexOf:

str.indexOf(searchStr, lastMatch + searchStr.length);

I add searchStr.length to prevent overlapping matches, e.g. searching for abab in abababacccc, there will be only 1 match found if I add searchStr.length. Change it to + 1 if you want to find all matches, regardless of overlapping.

Full example:

var lastMatch;
var result = [];

if ((lastMatch = str.indexOf(searchStr)) >= 0) {
    result.push(lastMatch);

    while ((lastMatch = str.indexOf(searchStr, lastMatch + searchStr.length)) >= 0) {
        result.push(lastMatch);
    }
}

RegExp.exec

This is to demonstrate the usage. For fixed string, use String.indexOf instead - you don't need the extra overhead with RegExp in fixed string case.

As an example for RegExp.exec:

// Need g flag to search for all occurrences
var re = /jack/g;
var arr;
var result = [];

while ((arr = re.exec(str)) !== null) {
    result.push(arr.index);
}

Note that the example above will give you non-overlapping matches. You need to set re.lastIndex if you want to find overlapping matches (no such thing for "jack" as search string, though).

Upvotes: 2

Sergi0
Sergi0

Reputation: 1096

you can use indexof method in a loop

var pos = foo.indexOf("jack");
while(pos > -1) {
     pos = foo.indexOf("jack", pos+1);
}

Upvotes: 4

Related Questions