【发布时间】:2013-08-20 02:32:25
【问题描述】:
我需要检查一组字符串中的任何一个是否存在于一个主字符串中。
目前我正在使用带有 for 循环的数组来检查每个字符串。
有没有更简单的方法,比如我遇到的红宝石解决方案 - How to compare a string against multiple other strings
["string1","string2","string3"].include? myString
【问题讨论】:
-
一个正则表达式?
RegExp(strings.join('|')).test(myString) -
不幸的是,这个 string19 将为 string1 返回 true 但这有效:matchfound = RegExp("^(" +strings.join("|")+")$").test(myString);基本上检查 ^(string1|string2|string3)$ 而不是 string1|string2|string3
标签: javascript