7

I want to test a given string against 20 or so regular expressions. What's a clean way to do this in Javascript? I'm more concerned about clean code and readability than efficiency (but I don't want it to be super slow either).

Right now I have:

if (href.indexOf('apple.com') > -1 || href.indexOf('google.com') > -1 || href.indexOf('yahoo.com') > -1 || href.indexOf('facebook.com') > -1) { performDarkMagic() } 

But it's going to start looking kind of messy as that list grows. Maybe I could just create an array of regular expressions and execute something like _.any() and apply regex.test on each?

Edit: the strings/regexes to match may become more complicated, I just used simple URLs to make the example readable.

6 Answers 6

9

Use the test function for regular expressions.

More information on regular expressions here. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp

var re = /((google)|(facebook)|(yahoo)|(apple))\.com/; re.test( str ); // returns true or false; 

Test cases.

Live Demo Here: http://jsfiddle.net/rkzXP/1/

 var func = function( str ){ var re = /((google)|(facebook)|(yahoo)|(apple))\.com/; return re.test( str ); }; test("test for valid values", function() { equal( func("google.com"), true); equal( func("facebook.com"), true); equal( func("apple.com"), true); }); test("test for invalid values", function() { equal( func("googl.com"), false); equal( func("faceook.com"), false); equal( func("apple"), false); }); 

So you can rewrite your code as the following.

var re = /((google)|(facebook)|(yahoo)|(apple))\.com/; if( re.test(str) ){ performDarkMagic() } 
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, sorry it's for qunit. But it's not needed. Let me take it out.
4
var patterns = ['apple.com', 'google.com', 'yahoo.com', 'facebook.com', ...] var callFunc = false; patterns.forEach(function(item){ if(href.indexOf(item) > -1){ callFunc = true; break; } }); if(callFunc) { performDarkMagic(); } 

5 Comments

forEach ftw +1 /me waits for the people to caution against it working not working in IE.. le sigh.
@Bergi why would you downvote that? Its a much more elegant solution than the one you provided.. which doesn't look much different from the op's.
@Loktar: Because the OP needs some()!
@HunterMcMillen: some() (like in my answer). Read carefully what the OP's expression means. Your code does something quite different.
Ah I see now. @Bergi you should of put that originally :P. Your func will keep going @hunterMcMillen whereas some will stop and not continue execution. Touche @Bergi.
4

Yes, building an array and using .any() or .some() is just fine, especially when there will be more than 4 values:

if (["apple","google","yahoo","facebook"].some(host => href.includes(`${host}.com`)) { performLightMagic(); } 

Yet I can't see regexes there, there are just strings; so you could simplify using regex.test() to:

if (/apple\.com|google\.com|yahoo\.com|facebook\.com/.test(href)) { performLightMagic(); } 

or even

if (/(apple|google|yahoo|facebook)\.com/.test(href)) { performLightMagic(); } 

4 Comments

This would get out of hand and ugly very quickly the larger the list gets.
Hm, it's much more shorthand than array notation. Of course everything depends on reusability of that object...
Yeah in his question though he said he has to test against 20 or so. IMO an array is the cleaner looking method.
I never said it would not :-) And for independent regexes, as he mentioned, the array will be easier to build.
2

You could put each one in array then looping over each.

Comments

-1

How I would do it: factor it out into a function. Put each regex in an array, loop over them and return true the first time indexOf > -1. If you reach the end of the loop return false.

Comments

-2

Loop through a regex array and then do:

result=result AND (result_of_regex); next; 

after the loop return result

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.