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" ?

2 Answers 2

12

Use indexOf():

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

if(str.indexOf("JsonEdit")>=0){
    //do something you want
}
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
}

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

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