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
user1321237
2 Answers 2
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
Nagaraju Badaeni
9008 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
Joseph
120k31 gold badges186 silver badges238 bronze badges
2 Comments
ThiefMaster
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.
RobG
@ThiefMaster—overkill? Hardly, but
indexof is a lot faster (not that anyone would notice for a one-off).lang-js