2017-05-07 9 views
-1
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"); 
    } 

以下のことが可能か、さらに変数を作成してすべてのキーワードを検索する必要がありますか?search()で(または||)を使用できますか?

+2

可能な重複[あなたが.search(と複数の文字列を検索するにはどうすればよい)方法?](のhttp:// stackoverflowの.com/questions/12251197/how-do-you-search-multiple-strings-with-the-search-method) – JJJ

答えて

2

あなたは検索方法で正規表現を使用することができます。

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

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

+1

この回答に追加するには、複数の見出しをリストする必要はありませんこの正規表現には「s」があるかどうかに関係なく発生します。 –

+0

@Benediktありがとう –

+0

@Kuu Akuありがとう –

関連する問題