js中正则,需要处理:
<code>/uapp/?accessToken=75872da5-56b8-4fd3-96be-7ad69d56a6e4 </code>
所以去写:
<code>let accessTokenRegex = new RegExp("?accessToken=([\w\-]+)", "g"); </code>
结果(VSCode中)提示出错:
[eslint] Invalid regular expression: /?accessToken=([w-]+)/: Nothing to repeat. (no-invalid-regexp)
然后去运行,也是同样错误:
<code>app.js?e485:225 Uncaught SyntaxError: Invalid regular expression: /?accessToken=([w-]+)/: Nothing to repeat at new RegExp (<anonymous>) at App._this.handleRoute (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:2674), <anonymous>:199:30) at Router.render (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:910), <anonymous>:334:5) at renderComponent (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:263:38) at setComponentProps (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:244:103) at buildComponentFromVNode (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:331:13) at idiff (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:134:57) at innerDiffNode (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:178:21) at idiff (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:147:65) at innerDiffNode (eval at <anonymous> (bundle_a59bab4b9891d196e2e3.js:821), <anonymous>:178:21) </code>
然后改为:
<code>let accessTokenRegex = new RegExp("\?accessToken=([\w\-]+)", "g"); </code>
错误依旧。
js regex 问号
javascript,regex,正则表达式,question mark,问号
难道是js中的正则中,就无法去匹配问号这个字符??
js regex match question mark
escaping question mark in regex javascript – Stack Overflow
真是垃圾,要两个反斜杠:
<code>let accessTokenRegex = new RegExp("\\?accessToken=([\w\-]+)", "g"); </code>
和一般的写法不一样。
【总结】
js中,正则中想要匹配问号这个字符,需要两个反斜杠\\?才可以,比如:
<code>let accessTokenRegex = new RegExp("\\?accessToken=([\w\-]+)", "g"); </code>
才能匹配其中的问号,至少语法上才不会出错。
转载请注明:在路上 » 【已解决】js中正则匹配问号出错:Invalid regular expression Nothing to repeat