2012-04-27 11 views
0

私は変数があります:$ form.attr(「アクション」)次のいずれかが含まれていますjavascript変数に文字列が含まれているかどうかを確認するにはどうすればよいですか?

action="/Administration/Contents/JsonCreate" 
action="/Administration/Contents/JsonEdit" 
action="/Administration/Contents/JsonDelete" 

どうかの()ステートメントを使用して、私はそれが単語「JsonEdit」を含んでいるかどうかを確認することができますが?

答えて

11

使用indexOf()

var str = $form.attr('action'); 

if(str.indexOf("JsonEdit")>=0){ 
    //do something you want 
} 
1

RegExp testを使用して、何かがあるかどうかを確認できます。 testreturns true if a match is found、そうでない場合はfalseです。

if(/JsonEdit/.test(action)){ 
    //there is something 
} 
+3

あなただけの特定の静的な文字列を別の文字列内にあるかどうかを確認したい場合、私はやり過ぎ正規表現は適切ではないと言うが、思います。 – ThiefMaster

+0

@ ThiefMaster-overkill?難しいですが、 'indexof'ははるかに高速です(誰にも分かりません)。 – RobG