数组拆分时如何包含分隔符?

时间:2021-04-22 21:46:04

I want to split some string with regexp. But I can't get the result what I think. I heard javascript does not support lookbehind.

我想用regexp拆分一些字符串。但我无法得到我认为的结果。我听说javascript不支持lookbehind。

var str = "always on the go? if yes, slip-on this dress that comes with u- neck, long sleeves and loose fit. wear with slacks and beanie to finish the ensemble.↵↵- u-neck↵- long sleeves↵- loose fit↵- knee hemline↵- plain print"

var str =“总是在旅途中?如果是的话,可以穿上这件带有U领,长袖和宽松版型的连衣裙。穿上休闲裤和帽子来完成整体服装。-u-neck↵-长袖↵-宽松剪裁 - 膝盖下摆↵-平纹印花“

val.split(/(?:([\.,?;\n])\s?)/gi)

My current result is like this

我目前的结果是这样的

["always on the go", "?", "if yes", ",", "slip-on this dress that comes with u-neck", ",", "long sleeves and loose fit", ".", "wear with slacks and beanie to finish the ensemble", ".", "", " ", "- u-neck", " ", "- long sleeves", " ", "- loose fit", " ", "- knee hemline", " ", "- plain print"]

[“总是在旅途中”,“?”,“如果是的话”,“,”,“穿上这件带有U领的衣服”,“,”,“长袖和宽松的衣服”,“。” ,“穿着休闲裤和豆豆来完成整体”,“。”,“”,“”,“ - u-neck”,“”,“ - 长袖”,“”,“ - 宽松”,“” ,“ - 膝盖底边”,“”,“ - 普通打印”]

But I want my result to be like this

但我希望我的结果是这样的

array[0] = "always on the go?";
array[1] = "if yes,";
...
...
...
array[n] = '↵'
array[n] = '↵'
array[n] = '- u-neck'
...
...

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


Try this regular expression, it will keep your ,?. at the end of sentence.

试试这个正则表达式,它会保留你的,?在句末。

var str= "always on the go? if yes, slip-on this dress that comes with u-         neck,long sleeves and loose fit. wear with slacks and beanie to finish the ensemble.↵↵- u-neck↵- long sleeves↵- loose fit↵- knee hemline↵- plain print".

var arr= str. split(/([^\.,?\n]+[\.,?\n])/gi);

#2


The only way I can think of is writing your own prototype

我能想到的唯一方法是编写自己的原型

String.prototype.splitKeep = function(arr){
    if (!(arr instanceof Array)) throw('input array please');
    var value = this.toString();
    for (var i = 0; i < arr.length; i++){
        value = value.split(arr[i]).join(arr[i] + ":split:").trim();
    }
    value = value.split(":split:");
    //remove all newlines with characters
    for (var i = 0; i < value.length; i++){
        var item = value[i];
        if (item[item.length-1] === '\n' && item.length > 1){
            value[i] = item.substring(0, item.length-1);
        }
    }
    return value;
}

And to use this

并使用这个

str.splitKeep(['.', ',', '?', ';', '\n'])

#1


Try this regular expression, it will keep your ,?. at the end of sentence.

试试这个正则表达式,它会保留你的,?在句末。

var str= "always on the go? if yes, slip-on this dress that comes with u-         neck,long sleeves and loose fit. wear with slacks and beanie to finish the ensemble.↵↵- u-neck↵- long sleeves↵- loose fit↵- knee hemline↵- plain print".

var arr= str. split(/([^\.,?\n]+[\.,?\n])/gi);

#2


The only way I can think of is writing your own prototype

我能想到的唯一方法是编写自己的原型

String.prototype.splitKeep = function(arr){
    if (!(arr instanceof Array)) throw('input array please');
    var value = this.toString();
    for (var i = 0; i < arr.length; i++){
        value = value.split(arr[i]).join(arr[i] + ":split:").trim();
    }
    value = value.split(":split:");
    //remove all newlines with characters
    for (var i = 0; i < value.length; i++){
        var item = value[i];
        if (item[item.length-1] === '\n' && item.length > 1){
            value[i] = item.substring(0, item.length-1);
        }
    }
    return value;
}

And to use this

并使用这个

str.splitKeep(['.', ',', '?', ';', '\n'])