admin管理员组文章数量:1291144
I am looking for a regular expression which fulfil below conditions
- Min 1 and Max 50 char
- No spaces at the beginning and ending of string
- Allow only one space, dot between 2 words.
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?
I am looking for a regular expression which fulfil below conditions
- Min 1 and Max 50 char
- No spaces at the beginning and ending of string
- Allow only one space, dot between 2 words.
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?
Share Improve this question edited Oct 10, 2017 at 10:16 Rashmi Narware asked Oct 10, 2017 at 10:11 Rashmi NarwareRashmi Narware 411 silver badge5 bronze badges 4-
1
Try
/^(?=.{1,50}$)[a-z]+(?:['_.\s][a-z]+)*$/i
. – Wiktor Stribiżew Commented Oct 10, 2017 at 10:14 - I updated my question, underscore will be there in pattern – Rashmi Narware Commented Oct 10, 2017 at 10:17
- It would be good to have a set of test strings. Try them at regex101./r/TmJTBD/3 – Wiktor Stribiżew Commented Oct 10, 2017 at 10:18
- /^(?=.{1,50}$)[a-z]+(?:['_.\s][a-z]+)*$/i. This worked for me . Thanks you so much. – Rashmi Narware Commented Oct 10, 2017 at 10:55
1 Answer
Reset to default 9You 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.
本文标签: javascriptJs Regular expression for first nameStack Overflow
版权声明:本文标题:javascript - Js Regular expression for first name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741499182a2381977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论