Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for all instances of a string inside a string

Hello I am using indexOf method to search if a string is present inside another string. But I want to get all the locations of where string is? Is there any method to get all the locations where the string exists?

<html>
<head>
    <script type="text/javascript">
        function clik()
        {
            var x='hit';
            //document.getElementById('hideme').value ='';
            document.getElementById('hideme').value += x;
            alert(document.getElementById('hideme').value);
        }

        function getIndex()
        {
            var z =document.getElementById('hideme').value;
            alert(z.indexOf('hit'));
        }
    </script>
</head>
<body>
    <input type='hidden' id='hideme' value=""/>
    <input type='button' id='butt1' value="click click" onClick="clik()"/>
    <input type='button' id='butt2' value="clck clck" onClick="getIndex()"/>
</body>
</html>

Is there a method to get all positions?

like image 915
sai Avatar asked Jul 29 '10 18:07

sai


People also ask

How do you find all occurrences of string in a string Python?

finditer() The finditer function of the regex library can help us perform the task of finding the occurrences of the substring in the target string and the start function can return the resultant index of each of them.

How do you check for multiple substrings in a string?

Use the any() function to check if multiple strings exist in another string, e.g. if any(substring in my_str for substring in list_of_strings): . The any() function will return True if at least one of the multiple strings exists in the string.

How do you find the number of occurrences of a substring in a string?

The count() method returns the number of occurrences of a substring in the given string.

How do you search for a part of a string in Python?

The find() string method is built into Python's standard library. It takes a substring as input and finds its index - that is, the position of the substring inside the string you call the method on.


3 Answers

Try something like:

var regexp = /abc/g;
var foo = "abc1, abc2, abc3, zxy, abc4";
var match, matches = [];

while ((match = regexp.exec(foo)) != null) {
  matches.push(match.index);
}

console.log(matches);
like image 144
cic Avatar answered Sep 18 '22 15:09

cic


Here is a working function:

function allIndexOf(str, toSearch) {
    var indices = [];
    for(var pos = str.indexOf(toSearch); pos !== -1; pos = str.indexOf(toSearch, pos + 1)) {
        indices.push(pos);
    }
    return indices;
}

Use example:

> allIndexOf('dsf dsf kfvkjvcxk dsf', 'dsf');
[0, 4, 18]
like image 36
Congelli501 Avatar answered Sep 19 '22 15:09

Congelli501


I don't know if there's a built in function to do it. You could do it in a simple loop though:

function allIndexes(lookIn, lookFor) {
    var indices = new Array();
    var index = 0;
    var i = 0;
    while(index = lookIn.indexOf(lookFor, index) > 0) {
        indices[i] = index;
        i++;
    }
    return indices;
}
like image 22
Kiersten Arnold Avatar answered Sep 19 '22 15:09

Kiersten Arnold