有没有办法让JSLint对此正则表达式感到满意? [英] Is there a way to make JSLint happy with this regex?

查看:60
本文介绍了有没有办法让JSLint对此正则表达式感到满意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过JSLint运行JavaScript时,我从同一行代码中收到以下两个错误.

When running my JavaScript through JSLint, I get the following two errors from the same line of code.

Problem at line 398 character 29: Insecure '.'.

if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/))

Problem at line 398 character 41: Unescaped '^'.

if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/))

我了解到JSLint可能过于谨慎".我阅读了有关类似问题的评论,出于JSLint的目的,"regex"选项.

I understand that JSLint may be being "over-cautious". I read the comments on a similar question, Purpose of JSLint "disallow insecure in regex" option.

尽管如此,我还是希望拥有世界上最好的,并拥有一个有效的正则表达式,这也不会引起JSLint的抱怨.

Nonetheless, I would like to have the best of all worlds, and have a working regular expression that also doesn't cause JSLint to complain.

但是我在regex上失败了.

But I fail at regex.

是否可以使正则表达式查找至少一个特殊字符的存在,而不会导致JSLint抱怨呢?

Is it possible to make regular expression that looks for the presence of at least one special character, yet doesn't cause JSLint to complain?

推荐答案

那是字符类;您不需要分隔符(例如:逗号).您可以通过将插入号(^)和破折号(-)放在重要位置来清理正则表达式,从而不必逃脱它们.

That's a character class; you don't need a separator (eg: the commas). You can clean up the regex by placing the caret (^) and the dash (-) in strategic positions so they don't need to be escaped.

/[!@#$%^&*?_~()-]/

应该工作.您还可以使用非文字字符类:

Should work. You can also use the non-word character class:

/\W/

任何不是字母(a-zA-Z),数字(0-9)或下划线(_)的匹配.

That matches anything that's not a letter (a-zA-Z), number (0-9) or underscore (_).

这篇关于有没有办法让JSLint对此正则表达式感到满意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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