特殊字符的Javascript匹配功能 [英] Javascript match function for special characters

查看:90
本文介绍了特殊字符的Javascript匹配功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理此代码,并使用匹配"功能来检测密码强度.如何检测字符串中是否包含特殊字符?

I am working on this code and using "match" function to detect strength of password. how can I detect if string has special characters in it?

if(password.match(/[a-z]+/)) score++;
if(password.match(/[A-Z]+/)) score++;
if(password.match(/[0-9]+/)) score++;

推荐答案

如果您将!@#$%和ë表示为特殊字符,则可以使用:

If you mean !@#$% and ë as special character you can use:

/[^a-zA-Z ]+/

^表示它不是a-z或A-Z或空格.

The ^ means if it is not something like a-z or A-Z or a space.

如果您的意思是仅使用!@ $& $之类的东西,则使用

And if you mean only things like !@$&$ use:

/\W+/

\w匹配单词字符,\W匹配不单词字符.

\w matches word characters, \W matching not word characters.

这篇关于特殊字符的Javascript匹配功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆