如何检查字符串是否包含JavaScript中的子字符串数组中的文本? [英] How to check if a string contains text from an array of substrings in JavaScript?

查看:90
本文介绍了如何检查字符串是否包含JavaScript中的子字符串数组中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挺直的.在javascript中,我需要检查字符串是否包含数组中包含的任何子字符串.

Pretty straight forward. In javascript, I need to check if a string contains any substrings held in an array.

推荐答案

没有内置功能可以为您完成此操作,您必须为其编写一个函数.

There's nothing built-in that will do that for you, you'll have to write a function for it.

如果您知道这些字符串不包含任何正则表达式中特殊的字符,那么您可以作弊一点,像这样:

If you know the strings don't contain any of the characters that are special in regular expressions, then you can cheat a bit, like this:

if (new RegExp(substrings.join("|")).test(string)) {
    // At least one match
}

...这会为您要查找的子字符串(例如,one|two)创建一个正则表达式,该正则表达式是一系列 alternations ,并测试是否存在匹配项,但是如果任何子字符串包含正则表达式中特殊的任何字符(*[等),则必须先将其转义,最好只进行无聊的循环.

...which creates a regular expression that's a series of alternations for the substrings you're looking for (e.g., one|two) and tests to see if there are matches for any of them, but if any of the substrings contains any characters that are special in regexes (*, [, etc.), you'd have to escape them first and you're better off just doing the boring loop instead.

实时示例:

var substrings = ["one", "two", "three"];
var str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

在对问题的评论中,马丁询问ECMAScript5中新的Array.prototype.map方法. map并没有那么多帮助,但是some是:

In a comment on the question, Martin asks about the new Array.prototype.map method in ECMAScript5. map isn't all that much help, but some is:

if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    // There's at least one
}

实时示例:

var substrings = ["one", "two", "three"];
var str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

您只能在兼容ECMAScript5的实现中使用它,尽管它对于polyfill来说是微不足道的.

You only have it on ECMAScript5-compliant implementations, though it's trivial to polyfill.

2020年更新:some示例可以使用箭头功能(ES2015 +)进行简化,并且您可以使用includes而不是indexOf:

Update in 2020: The some example can be simpler with an arrow function (ES2015+), and you might use includes rather than indexOf:

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}

实时示例:

const substrings = ["one", "two", "three"];
let str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (substrings.some(v => str.includes(v))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(v => str.includes(v))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

甚至对它投掷bind,尽管对我来说箭头功能更具可读性:

Or even throw bind at it, although for me the arrow function is much more readable:

if (substrings.some(str.includes.bind(str))) {
    // There's at least one
}

实时示例:

const substrings = ["one", "two", "three"];
let str;

// Setup
console.log("Substrings: " + substrings.join(","));

// Try it where we expect a match
str = "this has one";
if (substrings.some(str.includes.bind(str))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(str.includes.bind(str))) {
    console.log("Match using '" + str + "'");
} else {
    console.log("No match using '" + str + "'");
}

这篇关于如何检查字符串是否包含JavaScript中的子字符串数组中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆