1

I have a variable: $form.attr('action') that contains one of the following:

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

How can I with an if () statement check to see if it contains the word "JsonEdit" ?

asked Apr 27, 2012 at 5:29

2 Answers 2

12

Use indexOf():

var str = $form.attr('action');
if(str.indexOf("JsonEdit")>=0){
 //do something you want
}
The Codesee
3,7937 gold badges44 silver badges80 bronze badges
answered Apr 27, 2012 at 5:31
Sign up to request clarification or add additional context in comments.

Comments

1

You can use RegExp test to check if you have something there. test returns true if a match is found, and false if not.

if(/JsonEdit/.test(action)){
 //there is something
}
answered Apr 27, 2012 at 5:30

2 Comments

I'd say a regex is not appropriate but overkill if you just want to check if a certain static string is inside another string.
@ThiefMaster—overkill? Hardly, but indexof is a lot faster (not that anyone would notice for a one-off).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.