I am looking for a regular expression which fulfil below conditions
I am using below expression which causes catastrophic backtracking issue. Expression -
/^[a-zA-Z]+(?:(?:|['_\. ])([a-zA-Z]*(\.\s)?[a-zA-Z])+)*$/
How can I prevent this issue?
You may use
/^(?=.{1,50}$)[a-z]+(?:['_.\s][a-z]+)*$/i
See the regex demo.
Details
^
- start of string(?=.{1,50}$)
- there must be 1 to 50 chars in the string[a-z]+
- 1 or more ASCII letters(?:['_.\s][a-z]+)*
- 0 or more sequences of
['_.\s]
- a '
, _
, .
or whitespace[a-z]+
- 1 or more ASCII letters$
- end of string/i
- a case insensitive modifier.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With