JSLint reports “Insecure ^” for my regex — what does that mean?

大城市里の小女人 提交于 2019-11-26 12:31:03

问题


I\'m trying to get my Javascript code 100% JSLint clean.

I\'ve got a regular expression:

 linkRgx = /https?:\\/\\/[^\\s;|\\\\*\'\"!,()<>]+/g;

JSLint reports:

 Insecure \'^\'

What makes the use of the negation of the character set \"insecure\" ?


回答1:


[^\s;|\\*'"!,()<>] matches any ASCII character other than the ones listed, and any non-ASCII character. Since JavaScript strings are Unicode-aware, that means every character known to Unicode. I can see a lot of potential for mischief there.

Rather than disable the warning, I would rewrite the character class to match the characters you do want to allow, as this regex from the Regular Expressions Cookbook does:

/\bhttps?:\/\/[-\w+&@#/%?=~|$!:,.;]*[\w+&@#/%=~|$]/g



回答2:


(answering my own question) I did some digging... JSLint documentation says:

Disallow insecure . and [^...]. in /RegExp/ regexp: true if . and [^...] should not be allowed in RegExp literals. These forms should not be used when validating in secure applications.

What I have done is disable the JSLint error for the offending line (as I'm not dealing with needing to be secure from potentially malicious user input:

/*jslint regexp: false*/
.... Javascript statement(s) ....
/*jslint regexp: true*/



回答3:


You should use:

/*jslint regexp: true*/
linkRgx = /https?:\/\/[^\s;|\\*'"!,()<>]+/g;
/*jslint regexp: false*/


来源:https://stackoverflow.com/questions/3039955/jslint-reports-insecure-for-my-regex-what-does-that-mean

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