Given an pattern by a user, such as [h][a-z][gho], and a string "gkfhxhk" I'm trying to see if the string contains the pattern.
The pattern means that the first letter would be 'h', the next letter can be any letter from a-z, and the 3rd letter can be either 'g' or 'h' or 'o'. This is supposed to work for any pattern. So another pattern could be [y][b-f][jm].
First I tried to use nested for loops to create a list of all possible strings but that didn't work. Then I tried using RegExp where I pass in the pattern but I'm not quite sure if I'm approaching that the right way:
let string = "gkfhxhk";
// pattern [h][a-z][gho]
let re = new RegExp('ha-zg|h');
let result = re.match(re);
This just returns a null in the result. Any ideas on how to do this?
/^h[a-z][g|h|o]$/should do the trick.let re = new RegExp('h[a-z][gho]');orlet re = /h[a-z][gho]/;