我有一个react应用程序,它是用create-react应用程序创建的。chrome上的一切都很完美,但该应用程序在safari上崩溃,safari控制台中出现以下错误:
"SyntaxError:无效的正则表达式:无效的组说明符名称">
控制台中显示的错误消息
供应商出现错误的行~main.chunk.js
我完全不确定该怎么解决这个问题。任何帮助都将不胜感激。非常感谢。
使用捕获组:
const regex = /{([$0-9a-zA-Z_]+)(?=.*})/g
const variable = []
let m;
while (m = regex.exec(segment)) {
variable.push(m[1])
}
请参阅正则表达式证明。
解释
--------------------------------------------------------------------------------
{ '{'
--------------------------------------------------------------------------------
( group and capture to 1:
--------------------------------------------------------------------------------
[$0-9a-zA-Z_]+ any character of: '$', '0' to '9', 'a'
to 'z', 'A' to 'Z', '_' (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of 1
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
.* any character except n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
} '}'
--------------------------------------------------------------------------------
) end of look-ahead