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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

Simple 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