Is there a way to make JSLint happy with this regex?

流过昼夜 提交于 2019-12-01 22:38:52

问题


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(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/))

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.

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.

But I fail at regex.

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?


回答1:


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/

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



来源:https://stackoverflow.com/questions/3757697/is-there-a-way-to-make-jslint-happy-with-this-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!