最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - search() string for multiple occurrences - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited Jul 10, 2019 at 6:38 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked May 12, 2013 at 1:18 AlexAlex 1,0553 gold badges17 silver badges32 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

you can use indexof method in a loop

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

Usage remendation

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

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;       
}
发布评论

评论列表(0)

  1. 暂无评论