0

I need a regex for a string containing characters only with zero or more spaces allowed in between. But preceding and trailing spaces are not allowed.

I came up with this regex: /^[a-zA-Z]+(\s*[a-zA-Z]+)*$/

But I also need total length of the string between 2 to 30.

For that I tried using Range Quantifier like : /(^[a-zA-Z]+(\s*[a-zA-Z]+)*){2,30}$/

But this does not seem to work. What could be wrong?

Abhinandan Khilari
  • 947
  • 3
  • 11
  • 26
  • Wiktor, the answer you referenced is for the question where quantifier is applied to an anchor. But this is not the case with my question. So, can you please remove the duplicate marker? – Abhinandan Khilari Jun 11 '19 at 09:46
  • Why are you putting the anchor `^` inside the brackets, it should be outside – Mobrine Hayde Jun 11 '19 at 10:30
  • Where do you see a quantifier applied to an anchor in the solution? All you need is to add a positive lookahead with a pattern like `.{2,30}$`: `/^(?=.{2,30}$)[a-zA-Z]+(?:\s+[a-zA-Z]+)*$/` (where the limiting quantifier is applied to the `.` pattern). – Wiktor Stribiżew Jun 11 '19 at 10:30
  • Thanks Wicktor, by the way by saying quantifier applied to an anchor, I mean it is in the question for the answer you referred and not in the answer itself. – Abhinandan Khilari Jun 11 '19 at 10:38

0 Answers0