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