给定字符串:
有趣的 -> A_gre$" [ "at -> 看起来 ->很棒/*54[ [有趣的" -> [ " -> [很棒的 -> yolo] -> 看起来]][很棒] -> a2afg34423*/- ->哟洛“-> [”
拆分为数组:
- 有趣的
- A_gre$' ['在
- 看起来
- 很棒/*54[[有趣的'->['->[很棒->yolo]->看起来]][很棒]
- a2afg34423*/-
- 哟洛'-> ['
正则表达式解决方案??!?
基本上,如果括号周围有引号,请避免括号作为打开/关闭分隔符的功能,否则请确保打开括号和关闭括号之间的文本无效。我将如何使用正则表达式实现这一目标?
我的解析器解决方案 测试
var s = "funny -> A_gre$' [ 'at -> looks -> great/\*54[ [funny ' -> [ ' ->"
+ "[great -> yolo] -> looks]][great] -> a2afg34423*/- -> yolo' -> [ '",
p = 0,
z = [0],
q = 0,
x = s.split('');
//Looking for \" not \'
for(var i = 0; i< x.length; i++){
var b = x[i],
c = x[i + 1],
q = b == "'" ? ++q : q,
p = !(q % 2) ? b == '[' ? ++p : b == ']' ? --p : p : p;
if(b == '-' && c == '>' && !p && !(q % 2))
z.push(i + 2);
if(i == x.length - 1){
z.push(x.length); x = [];
for(var u = 0; u < z.length; u++)
z[u + 1] !== undefined ?
x.push(s.substring(z[u], z[u + 2] !== undefined ?
z[u + 1]-2 : z[u + 1]).trim()) : 0;
}
}
console.log(x)
输出:
->>> [
"funny",
"A_gre$' [ 'at",
"looks",
"great/*54[ [funny ' -> [ ' -> [great -> yolo] -> looks]][great]",
"a2afg34423*/-",
"yolo' -> [ '"
]