in reply to Regex - Matching prefixes of a word
You can require a space or end of string after your word, and you can make parts of your word(s) optional using the (?:...)? construct:
/^an?$/
will match any string that starts with a and maybe has a b following.
/^a(?:n)?$/
is the same, but using grouping parentheses.
/^a(?:n(?:g)?)?$/
will match a, an or ang
As you can see, as the words get longer, the nesting gets deeper. Most likely, you will want to construct these word-matchers using some code instead of hand-crafting them.
|
|---|