【问题标题】:Can we use or (||) in search()?我们可以在 search() 中使用 or (||) 吗?
【发布时间】:2017-05-07 11:47:21
【问题描述】:
function functiont(){
    var a = document.getElementById("q").value;
    var b = a.toLowerCase();
    var x = b.search("news"||"headline"||"headlines");//is this possible?
    if(x != -1){
        $("p").text("here are the results");
    } else{
        $("p").text("sorry");
    }

是否可能发生以下事情,或者我必须创建更多变量并搜索所有关键字?

【问题讨论】:

标签: javascript


【解决方案1】:

可以在搜索方法中使用正则表达式:

b.search(/news|headline|headlines/);

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/search

【讨论】:

  • 要添加到此答案中,您不需要列出标题的复数,因为无论此正则表达式是否有“s”,都会发生匹配。
  • @Benedikt 非常感谢
  • @Kuu Aku 非常感谢
猜你喜欢
  • 2013-10-12
  • 2017-07-18
  • 2012-06-03
  • 2012-01-03
  • 1970-01-01
  • 2015-01-08
  • 2018-10-31
  • 2020-03-21
  • 2011-08-21
相关资源
最近更新 更多