在字符串中搜索字符串的所有实例

编程入门 行业动态 更新时间:2024-10-28 12:23:02
本文介绍了在字符串中搜索字符串的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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?

推荐答案

尝试类似:

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

更多推荐

在字符串中搜索字符串的所有实例

本文发布于:2023-06-09 06:28:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/596842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   实例

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!