我将 rex 设置为这个字符串“root[0][1][2]”。我想要这个字符串数组([root, 0, 1, 2]) 或 String(root.0.1.2)。
我尝试过 make/["["]|["]["]/g。但是,这个 rex ][ 字符串识别出包含 ["]。 而这个霸王龙 ["["][^"["]* 认出了整个括号。
/["["]|["]["]/g <- 1
或
/["["][^"["]*/g <- 2
结果2:根[0][1][2][3] => [0],[1],[2],[3]
结果 1:["]["] => 错误
最佳答案
使用负字符集匹配除 [
或 ]
之外的任何内容:
const str = 'root[0][1][2]';
const matches = str.match(/[^[\]]+/g);
console.log(matches);
关于javascript - 如何让rex对[]进行替换或拆分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712532/