最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Converting Java Regex to Javascript Regex - Stack Overflow

programmeradmin2浏览0评论

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
发布评论

评论列表(0)

  1. 暂无评论