If this is an exercise in using Regular Expressions, so you really want to use a Regular Expression to solve it, you can do something like this:
regex = /^(PL|PH|MH|M)$/
choices = ["PL", "PH", "M", "MH", "other", "invalid", "value"]
index = Math.floor(Math.random() * choices.length)
input = choices[index]
console.log("Testing", '"'+input+'"')
x = input.match(regex)
if (x) {
x = x[0]
}
console.log("Output:", x) // << one of the allowed strings, or null
Press [ (>) Run code snippet ] multiple times to see different output values.
0
solved REGEX – Get specific values [closed]