Possible Duplicate:
Using a string variable as regular expression
I have a var that contains all English words. I want to use regular exp in order to filter the words according to specific letter that are stored in a var.
var letter1 = "a";
var letter2 = "b";
var words= "aardvark aardwolf aaron aback abacus abaft abalone abandon abandoned abandonment abandons abase abased abasement abash abashed abate abated abatement abates abattoir";
I tried this in order to find all the words that have the letter "a" and "b", but it is not working:
document.writeln(words.match(/letter1 +/ + letter2 +/g));
letter1andletter2at the same time?<script type="text/javascript"> var abc = ["asdf","asfdf","sdgsdwvc","avdvcccv","acbfd","sdfsd","gvvfvbb","sdfge34"]; var pattern = /.*[ab].*/i; var res; for(a in abc) { res = abc[a].match(pattern); if(res!=null || res!=undefined) alert(res); } </script>