admin 管理员组文章数量: 1086019
I have a regular expression in java
^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!()*,/:;<>?\\\]\[\-_`{}~@#$%^&+=]).*$
It matches strings which having these conditions:
- at least 8 characters
- at least one digit
- at least one small letter
- at least one capital letter
at least one special character
[!()*,/:;<>?\\\]\[\-_`{}~@#$%^&+=]
How can I convert it into a JavaScript regex?
I have a regular expression in java
^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!()*,/:;<>?\\\]\[\-_`{}~@#$%^&+=]).*$
It matches strings which having these conditions:
- at least 8 characters
- at least one digit
- at least one small letter
- at least one capital letter
at least one special character
[!()*,/:;<>?\\\]\[\-_`{}~@#$%^&+=]
How can I convert it into a JavaScript regex?
Share Improve this question edited Jan 15, 2014 at 20:32 nhahtdh 56.8k15 gold badges129 silver badges164 bronze badges asked May 29, 2012 at 13:20 BilalBilal 2,7405 gold badges33 silver badges58 bronze badges3 Answers
Reset to default 3Simple solution with simple regex.
var re = /^(.{0,7}|\D+|[^a-z]+|[^A-Z]+|[^\^!@#$%&\*])$/;
if (!re.test(str)) {
alert('Matched');
}
Note that there are some missing special characters in my regex.
put two forward slashes around it:
var re = /<your_regex_here>/;
It already works in JavaScript:
var re = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!()*,/:;<>?\\\]\[\-_`{}~@#$%^&+=]).*$/;
re.test('abcdefgh0A$') // true
本文标签: Converting Java Regex to Javascript RegexStack Overflow
版权声明:本文标题:Converting Java Regex to Javascript Regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743985254a2513743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论