禁用不必要的转义字符:\ / no-useless-escape [英] Disable Unnecessary escape character: \/ no-useless-escape
问题描述
我有我的这个正则表达式,它将检查字符串是否包含链接或URL (即https://eslint.org/docs/rules/no-useless-escape)$c $ c>。使用此正则表达式
/(\b(https?| ftp | file):\ / \ / [-A-Z0-9 +& @#\ /%?=〜_ | !:,。;] * [-A-Z0-9 +& @#\ /%=〜_ |])/ ig
,在运行测试用例时遇到错误对不必要的转义字符\ / no-useless-escape
做出反应。如何禁用此错误,以便我继续测试用例并使用正则表达式。
I have this regex of mine that will check the string if it contains link or url (i.e. https://eslint.org/docs/rules/no-useless-escape)
. Using this regex /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig
, I've encountered and error while running my test cases in react about Unnecessary escape character: \/ no-useless-escape
. How to disable this eslint-error in order for me to proceed with my test case and use the regex.
感谢您的帮助!
推荐答案
这是中的
和 + /
[-A-Z0-9 + & @#\ /%?=〜_ |!:,。;] [-A-Z0-9 +& @#\ /%= 〜_ |]
(不是:\ / \ /
中的那些)。大多数字符不必在字符类(方括号)内转义。这应该等效: /(\b(https?| ftp | file):\ / \ / [-A-Z0-9 +& @#/%?=〜_ | !:,。;] * [-A-Z0-9 +& @#/%=〜_ |])/ ig
所以
参见 https://www.regular-expressions.info/charclass.html 了解更多信息,但相关部分:
It's the \/
in [-A-Z0-9+&@#\/%?=~_|!:,.;]
and [-A-Z0-9+&@#\/%=~_|]
(NOT the ones in :\/\/
). Most characters do not have to be escaped within a character class (square brackets). This should be equivalent: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig
So
See https://www.regular-expressions.info/charclass.html for more info, but the relevant part:
在大多数正则表达式中,字符类中唯一的特殊字符或元字符
是右括号[],反斜杠\,
尖号^和连字符-。通常的元字符是字符类中的常规
字符,不需要通过
反斜杠进行转义。要搜索星号或加号,请使用[+ *]。如果您转义字符
类中的常规元字符,则您的正则表达式将可以正常工作,但这样做会大大降低可读性。
In most regex flavors, the only special characters or metacharacters inside a character class are the closing bracket ], the backslash \, the caret ^, and the hyphen -. The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.
这篇关于禁用不必要的转义字符:\ / no-useless-escape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!