美文网首页
(?<=pattern)反向肯定预查编译报错:SyntaxErr

(?<=pattern)反向肯定预查编译报错:SyntaxErr

作者: Mr老朝 | 来源:发表于2020-03-03 19:03 被阅读0次

    问题出现

    var reg = /(?<=95|98|NT|2000)Windows/
    

    报错

    ERROR in ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!
    Module parse failed: \node_modules\babel-loader\lib\index.js!\node_modules\vue-loader\lib\selector.js?type=script&index=0!export.vue Error parsing regular expression: Invalid regular expression:  /(?<=95|98|NT|2000)Windows/: Invalid group (118:26)
    You may need an appropriate loader to handle this file type.
    SyntaxError: Error parsing regular expression: Invalid regular expression: /(?<=95|98|NT|2000)Windows/: Invalid group (118:26)
        at Parser.pp$4.raise (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2221:15)
        at tryCreateRegexp (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2773:46)
        at Parser.pp$7.readRegexp (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2834:7)
        at Parser.pp$7.readToken_slash (node_modules\webpack\node_modules\acorn\dist\acorn.js:2606:52)
        at Parser.pp$7.getTokenFromCode (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2732:19)
        at Parser.pp$7.readToken (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2477:17)
        at Parser.pp$7.nextToken (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2468:15)
        at Parser.pp$7.next (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2413:10)
        at Parser.pp.eat (\node_modules\webpack\node_modules\acorn\dist\acorn.js:536:12)
        at Parser.pp$1.parseVar (\node_modules\webpack\node_modules\acorn\dist\acorn.js:1033:18)
        at Parser.pp$1.parseVarStatement (\node_modules\webpack\node_modules\acorn\dist\acorn.js:917:10)
        at Parser.pp$1.parseStatement (\node_modules\webpack\node_modules\acorn\dist\acorn.js:706:19)
        at Parser.pp$1.parseBlock (\node_modules\webpack\node_modules\acorn\dist\acorn.js:981:25)
        at Parser.pp$3.parseFunctionBody (\node_modules\webpack\node_modules\acorn\dist\acorn.js:2105:24)
        at Parser.pp$1.parseFunction (\node_modules\webpack\node_modules\acorn\dist\acorn.js:1065:10)
        at Parser.pp$3.parseExprAtom (\node_modules\webpack\node_modules\acorn\dist\acorn.js:1810:19)
     @ ./src/archer/curd-export.vue 7:2-108
    

    分析

    从报错信息我们看到是babel-loader报出来的,我把var reg = /(?<=95|98|NT|2000)Windows/chrome的控制台运行完全没有问题,那么我们的正则没有问题,问题就处在编译器不支持

    解决

    使用构造函数,这样反向肯定预查作为字符串参数传进去RegExp的构造函数,就不会走babel-loader的编译,不报错了

    var a = new RegExp('(?<=95|98|NT|2000)Windows')
    

    相关文章

      网友评论

          本文标题:(?<=pattern)反向肯定预查编译报错:SyntaxErr

          本文链接:https://www.haomeiwen.com/subject/ndyklhtx.html