views:

152

answers:

6

How can i check if a variable has something in it? I tried checking if a .match() returned null but that wasn't useful for checking with an OR?

All ideas would be gratefully appreciated!

+2  A: 

Try this:

var myString = 'I love cheese';
var isMatch = new RegExp(/(cheese|cake)/i).test(myString); //should return true
Rex M
No, it should not return true. It should return an array of matches.
Guffa
Thanks, I always mix match and test.
Rex M
The parentheses here are redundant!
Peter Boughton
Also worth pointing out that the `i` flag makes it case-insensitive, which may or not be desired depending on the exact needs.
Peter Boughton
A: 

Don't you just want the following?

var result = input.match("cheese") || input.match("cake");
Noldorin
Please explain why this deserves a down-vote.
Noldorin
+1 since it does the job, not sure why @anon downvoted.
meder
Using a regex with or, and a single regex, will be more performant than doing do matches (even though they have the same result). Note that I didn't downvote it, but I prefer using the proper regex form.
AlBlue
@Noldrin Because you don't need `match` where `test` suffices. 1) `match` takes a performance hit by creating an array to hold matched strings (if successful); `test` doesn't. 2) `match` returns array or `null`, not boolean (as `test` does), so you would probably still need to type-convert the result. 3) there's no need to call `match` twice when single regex does it just fine (as in @Gyoshev example). `/cake|cheese/.test(input)` is shorter, simpler and more efficient. Why would you want to use `match` here?
kangax
@kangax: In reply to: 1. small performance hit. 2. null gets imlicitly converted to `false` and non-null to `true`, so no problem there. 3. using two matches is arguably more readable.
Noldorin
@Noldorin I don't see implicit type conversion in your example. Mind explaining? Whether performance is relevant or not here is rather subjective, of course. My point is that `test` exists for exactly these kind of purposes, and using match is simply using the wrong tool for the job. I can agree with you about double match being more readable (especially for someone not familiar with regex).
kangax
@kangax: Perhaps `test` is more appropiate, but my solution is still viable, so I don't see how it deserves a down-vote.
Noldorin
I'm tempted to down-vote just for the whining. :P
Peter Boughton
@Peter: For the "whining"? You obviously have no concept of what a rational discussion is. Might be wise to not to taunt users at least until you have higher rep than them.
Noldorin
Noldorin, there's a difference between taunting and humour, one that the smiley at the end should make obvious - and in any case SO rep points have no impact on whether or not I'll make a comment. Rex made the same mistake, but he admitted that test was the correct solution and fixed his answer, whilst you're stubbornly sticking to match and are unhappy that someone disagrees with this. \*shrug\*
Peter Boughton
@Peter: Sorry, but I didn't interpret that as humour. The "sticking tongue out" smiley isn't exactly polite. I referred to rep points mainly to illustrate that you shouldn't assume you know the etiquette of SO. Oh, and it's still not a "mistake".
Noldorin
Saying this, I don't really want to argue any morpe. Let's agree to disagree. :)
Noldorin
+5  A: 

/cheese|cake/.test(a)

you could add i at the end of the regex if you want to test case-insensitively

Alexander Gyoshev
+1 not only for `|`, but because `test(...)` is more suitable than `match(...)` in this case.
outis
A: 

var matched = /cheese|cake/i.test('cheese cake'); alert(matched)

meder
+2  A: 

As you only got answers involving regular expressions, here is the plain string operation solution:

var hasMatch = input.indexOf('cheese') != -1 || input.indexOf('cake') != -1;
Guffa
+1  A: 

all expressions posted so far would also match "cheeseburger" and "cakewalk". Don't know if this is desirable or not, just in case here's the version that doesn't:

alert(/\b(cheese|cake)\b/i.test("cheese and cake")) // true
alert(/\b(cheese|cake)\b/i.test("cheeseburger and pancake")) // false
stereofrog