I have to validate a word that can start with y
, g
or h
.
For example, it can be yugo...
, hugo...
or gugo...
.
var original = "gugoss",
regex = /[ygh]/,
coincid = original.match(regex);
console.log(coincid);
How can I make it match 1 of those letters at the start of the String, regardless of the rest of the characters it contains?
Match finds for you the string within the string you supply that matches the regular expression you're passing to it and returns it as an array of objects (if found).
To do what you indicate in your question, you can use test() from the regular expression and passing the string to be validated as a parameter. The following code returns true if the string you provide starts with y, s or m .
NOTE: Note that it is necessary to use
^
to tell it to search, in this case, at the beginning of the string . If you omit it, it will return true if the string has any of those regex characters in the whole body.