如何使用.search()方法搜索多个字符串? [英] How do you search multiple strings with the .search() Method?
本文介绍了如何使用.search()方法搜索多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.search方法只能搜索一个字符串吗?或者有没有办法获得一个.search命令一次搜索多个字符串? (顺便说一句,我尝试了以下代码: y.search(c,b,a)
,其中y是文本框输入。)
Can the .search Method only search for one string? Or is there a way to get one single .search command to search for multiple strings at once? (By the way, I tried the following code:y.search("c", "b", "a")
, where y is an text box input.)
推荐答案
您不能使用多个参数,但可以使用带有或运算符的正则表达式( |
)如下:
You can't use multiple arguments, but you can use a regex with the or operator (|
) like so:
y.search(/c|b|a/);
此方法适用于多个字符长的字符:
This method will work for characters that are more than one character long, as well:
y.search(/string1|string2|string3/);
这篇关于如何使用.search()方法搜索多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文