在javascript中,如何在数组中搜索子字符串匹配
我需要在javascript中搜索数组。搜索将只搜索要匹配的字符串的一部分,因为该字符串将有附加编号分配给它。然后,我需要返回成功匹配的数组元素和完整字符串 i、 e 我需要搜索包含在javascript中,如何在数组中搜索子字符串匹配,javascript,arrays,search,Javascript,Arrays,Search,我需要在javascript中搜索数组。搜索将只搜索要匹配的字符串的一部分,因为该字符串将有附加编号分配给它。然后,我需要返回成功匹配的数组元素和完整字符串 i、 e 我需要搜索包含“id-”的数组元素,还需要提取元素中的其余文本(即“id-3-text”) 谢谢在您的具体情况下,您只需使用一个无聊的旧柜台即可: var index, value, result; for (index = 0; index < windowArray.length; ++index) { valu
“id-”
的数组元素,还需要提取元素中的其余文本(即“id-3-text”
)
谢谢在您的具体情况下,您只需使用一个无聊的旧柜台即可:
var index, value, result;
for (index = 0; index < windowArray.length; ++index) {
value = windowArray[index];
if (value.substring(0, 3) === "id-") {
// You've found it, the full text is in `value`.
// So you might grab it and break the loop, although
// really what you do having found it depends on
// what you need.
result = value;
break;
}
}
// Use `result` here, it will be `undefined` if not found
在没有hasOwnProperty
和属性的循环中,小心天真的for!isNaN(parseInt(键,10))
checks
主题外:
另一种写作方式
var windowArray = new Array ("item","thing","id-3-text","class");
是
…这对你来说打字更少,也许(这一点是主观的)更容易阅读。这两条语句的结果完全相同:一个包含这些内容的新数组。另一种可能性是
var res = /!id-[^!]*/.exec("!"+windowArray.join("!"));
return res && res[0].substr(1);
如果您可以使用一个特殊的字符分隔符(这里我使用“!”),数组是常量或大部分常量(因此连接可以计算一次或很少),并且完整字符串的长度不会比搜索的前缀长太多,那么IMO可能会有意义。ref:
这里给出的解决方案是通用的,与不同的是,它需要先前的解析来标识要与其连接的字符串字符串()
,该字符串不是任何数组字符串的组件。
另外,在该代码中/!id-[^!]*/
更正确,/![^!]*id-[^!]*/
以适应问题参数:
“搜索数组…”(指字符串或数字,而不是函数、数组、对象等)
“仅匹配字符串的一部分”(匹配可以是任意位置)
“返回……匹配的……元素”(单数,而不是全部,如“……元素”)
“带完整字符串”(包括引号)
。。。NetScape/FireFox解决方案(参见下面的JSON
解决方案):
或
显示“x”!x'\“id-2”
也许突袭阵列以找到所有匹配项是“更干净的”
/* literally (? backslash star escape quotes it!) not true, it has this one v */
javascript: /* purely functional - it has no ... =! */
RA = ["x'!x'\"id-2",'\' "id-1 "', "item","thing","id-3-text","class" ] ;
function findInRA(ra,id){
ra.unshift(void 0) ; /* cheat the [" */
return ra . toSource() . match( new RegExp(
'[^\\\\]"' + '([^"]|\\\\")*' + id + '([^"]|\\\\")*' + '[^\\\\]"' ,
'g' ) ) ;
}
alert( findInRA( RA, 'id-' ) . join('\n\n') ) ;
显示:
"x'!x'\"id-2"
"' \"id-1 \""
"id-3-text"
["x'!x'\"id-2"
,"' \"id-1 \""
,"id-3-text"
显示:
"x'!x'\"id-2"
"' \"id-1 \""
"id-3-text"
["x'!x'\"id-2"
,"' \"id-1 \""
,"id-3-text"
注意:剪切并粘贴任何javascript:
行作为即时模式URI(至少,在FireFox中最多?),首先使用javascript:
作为URI方案或协议,其余部分作为JS标签。有关一些替代方案及其效率的有趣研究,请参阅John Resig最近的帖子:
(这里讨论的问题略有不同,haystack元素是针的前缀,而不是针的前缀,但大多数解决方案都很容易适应。)如果您能够在项目中使用,那么数组函数将使这一点变得简单:
// find all strings in array containing 'thi'
var matches = _.filter(
[ 'item 1', 'thing', 'id-3-text', 'class' ],
function( s ) { return s.indexOf( 'thi' ) !== -1; }
);
迭代器函数可以做任何你想做的事情,只要它为匹配返回true。非常好用
更新2017-12-03:
这是一个非常过时的答案。可能不是大批量中性能最好的选项,但它可以写得更简洁,并使用本机ES6数组/字符串方法,如.filter()
和。includes()
现在:
// find all strings in array containing 'thi'
const items = ['item 1', 'thing', 'id-3-text', 'class'];
const matches = items.filter(s => s.includes('thi'));
注意:没有实现这一点的最简单的香草javascript代码是
var windowArray = ["item", "thing", "id-3-text", "class", "3-id-text"];
var textToFind = "id-";
//if you only want to match id- as prefix
var matches = windowArray.filter(function(windowValue){
if(windowValue) {
return (windowValue.substring(0, textToFind.length) === textToFind);
}
}); //["id-3-text"]
//if you want to match id- string exists at any position
var matches = windowArray.filter(function(windowValue){
if(windowValue) {
return windowValue.indexOf(textToFind) >= 0;
}
}); //["id-3-text", "3-id-text"]
只需在普通的oldindexOf
arr.forEach(function(a){
if (typeof(a) == 'string' && a.indexOf('curl')>-1) {
console.log(a);
}
});
这里的人把这件事弄得太难了。只需做以下几件事
myArray.findIndex(element => element.includes("substring"))
是一个ES6高阶方法,它遍历数组的元素并返回与某些条件匹配的第一个元素的索引(作为函数提供)。在本例中,我使用ES6语法声明高阶函数。element
是函数的参数(可以是任何名称)胖箭头声明了一个匿名函数(不需要用大括号括起来,除非它占用多行)
在findIndex()
中,我使用非常简单的includes()
方法检查当前元素是否包含所需的子字符串
下面是您期望的代码片段,它为您提供了所有匹配值的数组-
var windowArray=新数组(“项”、“物”、“id-3-text”、“类”);
var结果=[];
windowArray.forEach(val=>{
if(val&&val.includes('id-')){
结果:推送(val);
}
});
console.log(结果);
使用字符串匹配筛选数组。这很简单,只需一行代码。我认为这可能会对您有所帮助。我遇到了类似的问题。如果您的数组如下所示:
var array = ["page1","1973","Jimmy"];
当获得匹配项时,可以执行一个简单的“for”循环来返回数组中的实例
var c;
for (i = 0; i < array.length; i++) {
if (array[i].indexOf("page") > -1){
c = i;}
}
var-c;
对于(i=0;i-1){
c=i;}
}
我们创建一个空变量c来承载我们的答案。
然后,我们在数组中循环查找数组对象(例如“page1”)与indexOf(“page”)匹配的位置。在本例中,它是0(第一个结果)
如果您需要进一步的支持,很乐意扩展。这对我很有用
const filterData = this.state.data2.filter(item=>((item.name.includes(text)) || (item.surname.includes(text)) || (item.email.includes(text)) || (item.userId === Number(text))) ) ;
从给定数组获取子字符串数组的最简单方法是使用过滤器,包括:
myArray.filter(element => element.includes("substring"));
上面的一个将返回一个子字符串数组
myArray.find(element => element.includes("substring"));
上面的一个将返回数组中的第一个结果元素
myArray.findIndex(element => element.includes("substring"));
上面的函数将返回数组中第一个结果元素的索引。使用此函数搜索子字符串项
myArray.findIndex(element => element.includes("substring"));
函数检查项(arrayItem、searchItem){
返回arrayItem.findIndex(元素=>element.includes(searchItem))>=0
}
函数getItem(arrayItem,getItem){
返回arrayItem.filter(元素=>element.includes(getItem))
}
var arrayItem=[“项目”、“事物”、“id-3-text”、“类”];
日志(检查项(arrayItem,“id-”))
console.log(检查项(arrayItem,“vivek”))
log(getItem(arrayItem,“id-”)
我创建了一个简单易用的库(),用于处理对象,但也可以在您的情况下使用:
search(windowArray.map(x => ({ key: x }), ["key"], "SEARCH_TEXT").map(x => x.key)
使用t
const filterData = this.state.data2.filter(item=>((item.name.includes(text)) || (item.surname.includes(text)) || (item.email.includes(text)) || (item.userId === Number(text))) ) ;
myArray.filter(element => element.includes("substring"));
myArray.find(element => element.includes("substring"));
myArray.findIndex(element => element.includes("substring"));
search(windowArray.map(x => ({ key: x }), ["key"], "SEARCH_TEXT").map(x => x.key)