我想通过空格和特殊字符来滑动文本,但我想保留这些字符。我想要分离的文本是CSS格式的。举个例子:
.box {
background: red;
color: white;
}
我已经像这样以数组格式排列了
[ " .box { " ]
[ " background: red; " ]
[ " } " ]
[ " color: white; " ]
我想用空格和这些字符“:”和“;”分割每个字符串
所以我会得到这样的结果:
[".box"] ["{"]
["background"] [":"] ["red"] [";"]
["color"] [":"] ["white"] [";"]
["}"]
我想记住语法,以下是一些可能的情况:
background: red; -> ["background"] [":"] ["red"] [";"]
background : red ; -> ["background"] [":"] ["red"] [";"]
backg round : red ; -> ["backg"] ["round"] [":"] ["red"] [";"]
提前致谢!
编辑
关于重复的问题,我正在研究其他帖子的答案,它可能对我的具体情况有帮助,尽管如此,讨论的问题可能是在解析 CSS 之外的不同其他上下文中。请考虑一下,谢谢您的引用。
最佳答案
也许是这样的
var originalText = `.box {
background: red;
color: white;
}`;
var parsed = originalText
.split(/( |:|;|\n)/g) // split and maintain by using capturing group
.filter(p=>p.trim()); // keep only non whitespace elements
console.log(parsed);
关于javascript - 用空格或字符分隔文本并包含该字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47613363/