javascript 如何使用java脚本搜索字符串中的多个文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16851094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search for multiple texts in a string, using java script?
提问by MHS
I have a string, an array of words to be searched for:
我有一个字符串,一个要搜索的单词数组:
strng = "I have been working here since last six months"
text = ["since", "till", "until"]
result = "since"
I want to search for every word in array, in strng
and when any of it is found in the strng, it must be assigned to result. how to do it?
I am using .search()
for searching a single word, but how to search for multiple words? please help.
I am a Newbie.
我想搜索数组中的每个单词,strng
当在 strng 中找到任何单词时,必须将其分配给结果。怎么做?
我正在使用.search()
搜索单个单词,但如何搜索多个单词?请帮忙。
我是新手。
回答by joerx
You can either loop over your array of keywords or use a regular expression. The former is simple, the latter is more elegant ;-)
您可以遍历关键字数组或使用正则表达式。前者简单,后者更优雅;-)
Your regex should be something like "/since|till|until/", but I'm not 100% sure right now. You should research a bit about regexes if you're planning to use them, here's a starter: http://www.w3schools.com/js/js_obj_regexp.asp
您的正则表达式应该类似于“/since|till|until/”,但我现在不是 100% 确定。如果你打算使用它们,你应该研究一下正则表达式,这是一个初学者:http: //www.w3schools.com/js/js_obj_regexp.asp
EDIT: Just tried it and refreshed my memory. The simplest solution is using .match(), not search(). It's boils down to a one-liner then:
编辑:刚刚尝试并刷新了我的记忆。最简单的解决方案是使用 .match(),而不是 search()。它归结为一个单行然后:
strng.match(/since|till|until/) // returns ["since"]
Using .match() gives you an array with all occurrences of your pattern, in this case the first and only match is the first element of that array.
使用 .match() 为您提供一个包含所有出现的模式的数组,在这种情况下,第一个也是唯一的匹配项是该数组的第一个元素。
回答by schnill
I think regular expression is the simple solution for that. Use match() instead of search().
我认为正则表达式是解决这个问题的简单方法。使用 match() 而不是 search()。
if you write it as,
如果你把它写成
strng = "I have been working here since last six months"
text = /(since)|(till)|(here)/
result= strng.match(text); // result will have an array [since,since,]
correct solution is to write,
正确的解决办法是写,
result=strng.match(text)[0];
回答by 999k
You can loop through each item in text
with .search()
like this
你可以通过每个项目的循环text
与.search()
这样的
for (var i=0; i < text.length; i++)
{
if (-1 != strng.search(text[i]))
{
result = text[i];
break;
}
}
回答by BeNdErR
You can do like this:
你可以这样做:
var result;
for(var x = 0; x < text.length; x++){
if(strng.indexOf(text[x]) > -1){
result = text[x];
break;
}
}
alert(result);
working example here: http://jsfiddle.net/j5Y5d/1/
这里的工作示例:http: //jsfiddle.net/j5Y5d/1/
回答by Premshankar Tiwari
Do it like this...
像这样做...
strng = "I have been working here since last six months";
text = ["since", "till", "until"];
results = []; // make result as object
for(var i=0; i<=text.length; i++){
if(strng.indexOf(text[i]) != -1)
results.push(text[i]);
}
To make indexOf work in IE, put this code in your script
要使 indexOf 在 IE 中工作,请将此代码放入您的脚本中
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
};
}