yhtan
yhtan

Reputation: 71

Regex limit full matches with specific expression

My Regex matches words and only allow one space in between. Below is my Regex

^([a-zA-Z\u4e00-\u9fa5]+ )*[a-zA-Z\u4e00-\u9fa5]+$

This works fine but i want to limit the matches including space, how can i do this? For Example:

{2,30}

And this does not work

^(([a-zA-Z\u4e00-\u9fa5]+ )*[a-zA-Z\u4e00-\u9fa5]+$){2,30}

Upvotes: 0

Views: 59

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

You could use a positive lookahead assertion to check the length:

^(?=.{2,30}$)([a-zA-Z\u4e00-\u9fa5]+ )*[a-zA-Z\u4e00-\u9fa5]+$
  ^^^^^^^^^^

Explore the regex in the demo below. I removed the unicode characters to make things simpler.

Demo

Upvotes: 1

Related Questions