admin管理员组文章数量:1336434
I don't get how I can limit a capturing group.
If I have a regex like this:
/^(\w{2,}\s\w{2,}){4,15}$/
I would think that this would capture any string with:
- Exact two words,
- With each word least 2 characters long,
- And whole string no longer than 15 characters.
But the limiting of my capturing groups doesn't work. Can I limit capturing groups at all?
PS. I am using JavaScript to test the regexes in my examples.
I don't get how I can limit a capturing group.
If I have a regex like this:
/^(\w{2,}\s\w{2,}){4,15}$/
I would think that this would capture any string with:
- Exact two words,
- With each word least 2 characters long,
- And whole string no longer than 15 characters.
But the limiting of my capturing groups doesn't work. Can I limit capturing groups at all?
PS. I am using JavaScript to test the regexes in my examples.
Share Improve this question edited Jun 16, 2014 at 19:32 Amal 76.7k18 gold badges132 silver badges153 bronze badges asked Jun 13, 2014 at 17:31 NanoNano 1,3986 gold badges20 silver badges33 bronze badges1 Answer
Reset to default 8This lookahead based regex should work for you:
/^(?=.{4,15}$)\w{2,}\s\w{2,}$/
Working Demo
Your regex: ^(\w{2,}\s\w{2,}){4,15}$
basically means there should be between 4 to 15 instances of a string containing 2 words with at least 2 characters separated by a space
本文标签: javascriptHow to limit a regex capturing groupStack Overflow
版权声明:本文标题:javascript - How to limit a regex capturing group? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313059a2451263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论