如何使用 .search() 方法搜索多个字符串? [英] How do you search multiple strings with the .search() Method?

查看:16
本文介绍了如何使用 .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.)

推荐答案

您不能使用多个参数,但您可以使用带有 or 运算符 (|) 的正则表达式,如下所示:

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屋!

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