首页 > 解决方案 > Unnecessary escape character warning when running 'npm start' with React app

问题描述

I'm running my React app with npm start and App compiles but i'm getting this warning in terminal:

Line 24: Unnecessary escape character: \[ no-useless-escape

Here's the code it's referring to:

  validateEmail(email) {
    const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
  }

How do I address this warning?

标签: javascriptnode.jsreactjsnpmecmascript-6

解决方案


You need to add another slash / into your code like this

function validateEmail(email) {
    const re = /^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

推荐阅读