数组拆分时如何包含分隔符?
How to include separator when array split?
我想用正则表达式拆分一些字符串。
但是我无法得到我想的结果。
听说 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"
val.split(/(?:([\.,?;\n])\s?)/gi)
我现在的结果是这样的
["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"]
但我希望我的结果是这样的
array[0] = "always on the go?";
array[1] = "if yes,";
...
...
...
array[n] = '↵'
array[n] = '↵'
array[n] = '- u-neck'
...
...
我该怎么做?
试试这个正则表达式,它会让你的 ,?.在句末。
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);
我能想到的唯一方法是编写自己的原型
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;
}
并使用这个
str.splitKeep(['.', ',', '?', ';', '\n'])
我想用正则表达式拆分一些字符串。 但是我无法得到我想的结果。 听说 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"
val.split(/(?:([\.,?;\n])\s?)/gi)
我现在的结果是这样的
["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"]
但我希望我的结果是这样的
array[0] = "always on the go?";
array[1] = "if yes,";
...
...
...
array[n] = '↵'
array[n] = '↵'
array[n] = '- u-neck'
...
...
我该怎么做?
试试这个正则表达式,它会让你的 ,?.在句末。
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);
我能想到的唯一方法是编写自己的原型
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;
}
并使用这个
str.splitKeep(['.', ',', '?', ';', '\n'])