admin管理员组文章数量:1398111
I'm trying to validate a text field but, I'm getting an error for the below regex expression.
if (!/^[a-zA-Z0-9\\\/\*+;&%?#@!^()_="\-:~`|[\]\{\}\s]*$/i.test(e.target.value)) {
this.setState({
newFamilyName: e.target.value,
});
}
Do I need to add any more characters in the Regex so fulfill it.
I'm trying to validate a text field but, I'm getting an error for the below regex expression.
if (!/^[a-zA-Z0-9\\\/\*+;&%?#@!^()_="\-:~`|[\]\{\}\s]*$/i.test(e.target.value)) {
this.setState({
newFamilyName: e.target.value,
});
}
Do I need to add any more characters in the Regex so fulfill it.
Share Improve this question edited Nov 5, 2019 at 4:08 Mohamed Ibrahim Elsayed 2,9843 gold badges26 silver badges46 bronze badges asked Nov 4, 2019 at 22:02 tejasree vangapallitejasree vangapalli 891 silver badge11 bronze badges 2- I mean... if the escape character is "unnecessary", wouldn't that indicate removing characters rather than adding? – Kevin B Commented Nov 4, 2019 at 22:13
- I've edited my answer with a fix. You can mark it as solved if it helped. – Nicolas Hevia Commented Nov 6, 2019 at 5:11
2 Answers
Reset to default 4You have some unnecesary escape characters:
\/
\*
\{
\}
You can fix it:
if (!/^[a-zA-Z0-9\\/*+;&%?#@!^()_="\-:~`|[\]{}\s]*$/i.test(e.target.value)) {
this.setState({
newFamilyName: e.target.value,
});
}
Or disable the rule:
//eslint-disable-next-line
if (!/^[a-zA-Z0-9\\\/\*+;&%?#@!^()_="\-:~`|[\]\{\}\s]*$/i.test(e.target.value)) {
this.setState({
newFamilyName: e.target.value,
});
}
Unnecessary escape character means you have a \
in front of a character in your regex that you don't actually need.
I plugged the code you posted into https://eslint/demo and got:
1:20 - Unnecessary escape character: \/. (no-useless-escape)
1:22 - Unnecessary escape character: \*. (no-useless-escape)
1:47 - Unnecessary escape character: \{. (no-useless-escape)
1:49 - Unnecessary escape character: \}. (no-useless-escape)
Which means you didn't need the \
in front of the /
, *
, {
, or }
in your regex. Those characters don't need to be escaped since they appeared inside the []
group.
Unfortunately the "Fixed Code" thing on the eslint demo site didn't work, but the following code is how you would fix it
if (!/^[a-zA-Z0-9\\/*+;&%?#@!^()_="\-:~`|[\]{}\s]*$/i.test(e.target.value)) {
this.setState({
newFamilyName: e.target.value,
});
}
本文标签: javascriptGetting an ESLint Unnecessary escape characterStack Overflow
版权声明:本文标题:javascript - Getting an ESLint: Unnecessary escape character - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744169228a2593698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论