Ok, I want to combine two regular expressions. In the example below, I want to extract any word which has only the letters specified, then of that list, only words with another letter, r in this example.
I have read several articles on this forum.
One for example is: Combining regular expressions in Javascript
They say to combine the expressions with |, exp1|exp2. That's what I did, but I get words I shouldn't. How can I combine r1 and r2 in the example below?
Thanks for your help.
//the words in list form.
var a = "frog dog trig srig swig strog prog log smog snog fig pig pug".split(" "),
//doesn't work, can we fix?
     r = /^[gdofpr]+$|[r]+/,
    //These work.
    r1 = /^[gdofpr]+$/,
    r2 = /[r]+/,
    //save the matches in a list.
    badMatch = [], 
    goodMatch = [];
//loop through a.
for (var i = 0; i < a.length; i++) {
    //bad, doesn't get what I want.
    if (a[i].match(r)) badMatch[badMatch.length] = a[i];
    //works, clunky. Can we combine these?
    if (a[i].match(r1) && a[i].match(r2)) goodMatch[goodMatch.length] = a[i];
} //i
alert(JSON.stringify([badMatch, goodMatch])); //just to show what we got.
I get the following.
[
    ["frog", "dog", "trig", "srig", "strog", "prog"],
    ["frog", "prog"]]
Again, thanks.
 
     
    