-2

I have made a regex for matching the specific letters:

      a, ae, eo, e, eu, ya, yae, yeo, ye, yo, o, oe, wa, wae, wo, we, wi, yu, u, ui, i, oo, ah

This is the solution that I made a[eh]||e[ou]|o[eo]|u[i]|w[aoei]|y[aeou]|[aeiou]. Is there any alternative solution that I could use to improve its performance or a better solution for this?

  • This works but I am not sure how as I am quite new to regex. Could you explain this please. – Poorvi Yeluri Aug 21 '22 at 09:59
  • This pattern doesn't match any of the three-letter inputs like `yae`. On the other hand, it does match the empty string, because of `||`. – Jelaby Aug 21 '22 at 10:45
  • If your question actually is "what did this regex mean?", then see https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean – Jelaby Aug 21 '22 at 12:41
  • You could be using a prefix tree here. – Luatic Aug 21 '22 at 12:50

1 Answers1

0

I think, there is no other solution to create regex of such type of pattern. But you may create this regex dynamically like this way . . .

arr = ['a', 'ae', 'eo', 'e', 'eu', 'ya', 'yae', 'yeo', 'ye', 'yo', 'o', 'oe', 'wa', 'wae', 'wo', 'we', 'wi', 'yu', 'u', 'ui', 'i', 'oo', 'ah'];
s = ''
arr.forEach(x => s = s + (s ? '|' : '') + x)
reg = new RegExp(s, 'gi')
// /a|ae|eo|e|eu|ya|yae|yeo|ye|yo|o|oe|wa|wae|wo|we|wi|yu|u|ui|i|oo|ah/gi
Art Bindu
  • 769
  • 4
  • 14