如果数组值存在于字符串中(类似于regex)

时间:2022-03-01 01:03:07

How can I determine if a string contains one of the values from an array?

如何确定字符串是否包含数组中的一个值?

For example:

例如:

var a = ["abc","def","ghi"];

var s = "jskljfdkljflkjk abc jskfdjklsj";


for(var i=0;i<a.length;i++){
    if(/a[i]/.test(s)) alert(1);
}

This obviously doens't work... I know it's very possible though hahaha

这显然多恩不工作…虽然哈哈哈,我知道这很有可能

4 个解决方案

#1


3  

Your syntax for creating the regular expression is incorrect. That regex will only return true for a string "ai". And you're testing the regular expression against the array. I think what you meant to write is:

创建正则表达式的语法不正确。该regex将只对字符串“ai”返回true。你正在测试这个数组的正则表达式。我认为你想写的是:

if(RegExp(a[i]).test(s)) alert(1);

You would probably be better off just using indexOf in this case. It'll be faster and you won't need to escape any characters.

在这种情况下,你最好使用indexOf。它会更快,你不需要转义任何字符。

var a = ["abc","def","ghi"],
    s = "jskljfdkljflkjk abc jskfdjklsj";

for(var i = 0, l = a.length; i < l; i++)
    if(s.indexOf(a[i])+1) alert('string s contains a value from array a');

#2


1  

function doesStringContainElementFromArray(str, arr)
{
  for ( var i=0; i<arr.length; i++)
  {
    if ( str.indexOf(arr[i]) != -1 )
      return true;
  }
  return false;
}

#3


1  

Just use the "RegExp" function/constructor (if you really need regexps)

只需使用“RegExp”函数/构造函数(如果确实需要RegExp)

if (RegExp(a[i]).test(a)) {
  alert(1);
}

if you don't, just use .indexOf

如果你不知道,就用。indexof。

if (s.indexOf(a[i]) != -1) {
  alert("a[i]="+a[i]+" is matched in " + s);
}

#4


1  

You can use search method of JavaScript

可以使用JavaScript的搜索方法

var a = ["abc","def","ghi"];

var s  = "jskljfdkljflkjk abc jskfdjklsj";

for(var i=0;i<a.length;i++){

  if(s.search( a[i] ) != -1)
  {
     alert("found");
  }
}

#1


3  

Your syntax for creating the regular expression is incorrect. That regex will only return true for a string "ai". And you're testing the regular expression against the array. I think what you meant to write is:

创建正则表达式的语法不正确。该regex将只对字符串“ai”返回true。你正在测试这个数组的正则表达式。我认为你想写的是:

if(RegExp(a[i]).test(s)) alert(1);

You would probably be better off just using indexOf in this case. It'll be faster and you won't need to escape any characters.

在这种情况下,你最好使用indexOf。它会更快,你不需要转义任何字符。

var a = ["abc","def","ghi"],
    s = "jskljfdkljflkjk abc jskfdjklsj";

for(var i = 0, l = a.length; i < l; i++)
    if(s.indexOf(a[i])+1) alert('string s contains a value from array a');

#2


1  

function doesStringContainElementFromArray(str, arr)
{
  for ( var i=0; i<arr.length; i++)
  {
    if ( str.indexOf(arr[i]) != -1 )
      return true;
  }
  return false;
}

#3


1  

Just use the "RegExp" function/constructor (if you really need regexps)

只需使用“RegExp”函数/构造函数(如果确实需要RegExp)

if (RegExp(a[i]).test(a)) {
  alert(1);
}

if you don't, just use .indexOf

如果你不知道,就用。indexof。

if (s.indexOf(a[i]) != -1) {
  alert("a[i]="+a[i]+" is matched in " + s);
}

#4


1  

You can use search method of JavaScript

可以使用JavaScript的搜索方法

var a = ["abc","def","ghi"];

var s  = "jskljfdkljflkjk abc jskfdjklsj";

for(var i=0;i<a.length;i++){

  if(s.search( a[i] ) != -1)
  {
     alert("found");
  }
}