问题:
是否可以在不涉及regex的情况下使用javascript .split保留选定的分隔符?在下面的示例中,我使用node.js发送命令。
// A css text string.
var text_string = "div-1{color:red;}div-2{color:blue;}";
// Split by [}], removes the delimiter:
var partsOfStr = text_string.split('}');
// Printouts
console.log("Original: " + text_string); // Original.
console.log(partsOfStr); // Split into array.
console.log(partsOfStr[0]); // First split.
console.log(partsOfStr[1]); // Second split.输出:
Original: div-1{color:red;}div-2{color:blue;}
[ 'div-1{color:red;', 'div-2{color:blue;', '' ]
div-1{color:red;
div-2{color:blue;通缉行为:
我需要输出来包含分隔符}。结果行应该如下所示:
div-1{color:red};
div-2{color:blue};我确实发现了以下问题,但它没有使用javascript,而是使用regex:
发布于 2019-01-25 19:20:53
这里有一种使用replace的方法--尽管从技术上讲,这涉及到一个正则表达式。技术的方式几乎是迂腐的,因为它符合实际的字符串,只在斜杠之间,而不是引号之间。
var text_string = "div-1{color:red;}div-2{color:blue;}";
var partsOfString = text_string.replace(/;}/g, "};\n")
console.log(partsOfString);
https://stackoverflow.com/questions/54371433
复制相似问题