搜索字符串内字符串的所有实例

发布于 2024-09-12 17:55:00 字数 885 浏览 1 评论 0原文

您好,我正在使用 indexOf 方法来搜索一个字符串是否存在于另一个字符串中。但我想获取字符串所在的所有位置?有没有什么方法可以获取字符串存在的所有位置?

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

有没有办法获取所有位置?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骄兵必败 2024-09-19 17:55:00

尝试类似的方法:

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

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);
再浓的妆也掩不了殇 2024-09-19 17:55:00

这是一个工作函数:

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

使用示例:

> allIndexOf('dsf dsf kfvkjvcxk dsf', 'dsf');
[0, 4, 18]

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]
情绪操控生活 2024-09-19 17:55:00

我不知道是否有内置函数可以做到这一点。不过,您可以通过一个简单的循环来完成:

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

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;
}
新人笑 2024-09-19 17:55:00

您可以使用 indexOf('searchstring', ),使用返回的索引 'last time round' + 1 直到返回 -1。

You can use indexOf('searchstring', ), using the index returned 'last time round' + 1 until you get -1 back.

断念 2024-09-19 17:55:00

这是一个正则表达式的方法:

function positions(str, text) {
  var pos = [], regex = new RegExp("(.*?)" + str, "g"), prev = 0;
  text.replace(regex, function(_, s) {
    var p = s.length + prev;
    pos.push(p);
    prev = p + str.length;
  });
  return pos;
}

Here's a regex way to do it:

function positions(str, text) {
  var pos = [], regex = new RegExp("(.*?)" + str, "g"), prev = 0;
  text.replace(regex, function(_, s) {
    var p = s.length + prev;
    pos.push(p);
    prev = p + str.length;
  });
  return pos;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文