Trying to make regex JSLint compliant
I'm trying to make this regex:
var url_pattern = /(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\开发者_运维百科b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img;
JSLint compliant. It's complaining about the use of . and not escaping [ and ].
(Source: http://jmrware.com/articles/2010/linkifyurl/linkify.html)
edit: Not that big of a deal, I just did it in the backend anyway. I'd still like a version (since I'm building a rendering engine-y thing, but this isn't super urgent anymore).
Here is a version with escaped [
characters:
var url_pattern = /(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#\[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#\[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#\[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#\[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#\[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#\[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#\[\]@%]*)*[a-z0-9\-_~$()*+=\/#\[\]@%])/img;
It still complains about "insecure ^
", which is difficult to fix because it’s essentially implying that you should write out all the characters you want to match instead of listing the ones you don’t want to match. Which of course defeats the purpose of ^
. So you can ignore this warning.
Replace:
var url_pattern = /(\()((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\))|(\[)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\])|(\{)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\s'"\]])\s*['"]?|[^=\s]\s+)(\b(?:ht|f)tps?:\/\/[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\-._~!$&'()*+,;=:\/?#[\]@%]|$))&[a-z0-9\-._~!$'()*+,;=:\/?#[\]@%]*)*[a-z0-9\-_~$()*+=\/#[\]@%])/img;
with
var url_pattern = new RegExp("([\\.+])", "img"); // simplified for my sanity
With the string properly escaped, \
to \\
EDIT:
var a = new RegExp("(\\()((?:ht|f)tps?:\\/\\/[a-z0-9\\-._~!$&'()*+,;=:\\/?#[\\]@%]+)(\\))|(\\[)((?:ht|f)tps?:\\/\\/[a-z0-9\\-._~!$&'()*+,;=:\\/?#[\\]@%]+)(\\])|(\\{)((?:ht|f)tps?:\\/\\/[a-z0-9\\-._~!$&'()*+,;=:\\/?#[\\]@%]+)(\\})|(<|&(?:lt|#60|#x3c);)((?:ht|f)tps?:\\/\\/[a-z0-9\\-._~!$&'()*+,;=:\\/?#[\\]@%]+)(>|&(?:gt|#62|#x3e);)|((?:^|[^=\\s'\"\\]])\\s*['\"]?|[^=\\s]\\s+)(\\b(?:ht|f)tps?:\\/\\/[a-z0-9\\-._~!$'()*+,;=:\\/?#[\\]@%]+(?:(?!&(?:gt|#0*62|#x0*3e);|&(?:amp|apos|quot|#0*3[49]|#x0*2[27]);[.!&',:?;]?(?:[^a-z0-9\\-._~!$&'()*+,;=:\\/?#[\\]@%]|$))&[a-z0-9\\-._~!$'()*+,;=:\\/?#[\\]@%]*)*[a-z0-9\\-_~$()*+=\\/#[\\]@%])", "gim");
The above passes jslint
精彩评论