given this set of letters
xx | af | an | bf | bn | cf | cn
how can I see if, given two characters, they match against one of the above?
I could easily hardcode the solution with a switch case, but I think regex is a more elegant solution.
given this set of letters
xx | af | an | bf | bn | cf | cn
how can I see if, given two characters, they match against one of the above?
I could easily hardcode the solution with a switch case, but I think regex is a more elegant solution.
You wrote the regular expression yourself as stated previously, you could simplify it to...
var re = /xx|[abc][fn]/
Try this:
^(xx|af|an|bf|bn|cf|cn)$
xx => Correct
af => Correct
aff => Incorrect
kk => Incorrect
You can use this code:
// regex to match your words
var re = /\b(xx|af|an|bf|bn|cf|cn)\b/g;
// your text string
var str = 'as ww zx af ad we re an ana ';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}