Babeljs为正则表达式{m,n}量词产生意外错误

时间:2016-07-30 01:16:53

标签: javascript regex ecmascript-6 babeljs transpiler

我有以下内容:

const checkCode = code => {
    if( /^\+[0-9]{3}/[0-9]{3}[A-Z]+[0-9]{3}$/.test( code ))){
        return true;
    }
    //run more code...
}

Babel transpiler会产生意外的令牌错误,指向正则表达式的第一个 {,如:

if( /^\+[0-9]{
             ^

2 个答案:

答案 0 :(得分:1)

babel错误可能指向一个奇怪的标记,但你明显有一些拼写错误:

public static void main(String[] args) 
{
    try {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver has been found..");
    } catch (ClassNotFoundException ex) {
        System.out.println("Driver Not Found"+ex);
    }

    String url="jdbc:mysql://localhost/hms";
    String user="root";
    String password="";

    Connection con=null;

    try {
        con=DriverManager.getConnection(url, user, password);
        System.out.println("Driver is successfully loaded.");
    } catch (SQLException ex) {
        System.out.println("Something is not good.");
    }
}

答案 1 :(得分:0)

删除额外的括号后,编译器错误仍然存​​在。然后我逃脱了" /"在模式中,错误消失了。

由于javascript引擎使用" /"表示未转义的正则表达式的开始和结束" /"在模式内发送了错误的消息。为什么转换器将错误标记放在量词字符上还不清楚。