想简单写个压缩js文件的方法,在使用正则时遇到了问题。
let newStr = str.replace(/(?<!(var|let|const|function))\s/g, '');
该正则在浏览器上正常,但在node.js中就报错
let newStr = str.replace(/(?<!(var|let|const|function))\s/g, '');
^
SyntaxError: Invalid regular expression: /(?<!(var|let|const|function))\s/: Invalid group
at Object.<anonymous> (c:\Users\Administrator\Desktop\test2\test2.js:6:17)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3
应该是兼容性问题。
以前的js只支持先行断言,目前chrome也支持了后发断言。
如果你不用npm的话,可以升级node到10,我试过这个版本是支持的
如果要用npm的话,试一下8.12和9吧,这个我没试
反正8.9不行。
多嘴一句,js句末换行时可以不加分号,你这个正则遇到这种情况就不行了。
压缩js还是交给uglify.js吧