2010-12-22 217 views
36

如何拆分字符串而不删除分隔符?JS string.split()不删除分隔符

比方说,我有一个字符串: var string = "abcdeabcde";

当我做 var newstring = string.split("d"),我得到的是这样的:

["abc","eabc","e"]

但我想这一点:

["abc","d","eabc","d","e"]

当我试图做我的“split2”功能时,我全部纠缠在splice()和索引,“this”vs“that”以及... aargh!帮帮我! :d

+0

的可能重复的[字符串分割到阵列而不删除分隔符〕(http://stackoverflow.com/questions/24503827/split-string-into-array-without-deleting-分隔符) – BartoszKP 2014-07-01 15:11:58

回答

15

试试这个:

  1. 替换所有的 “d” 的情况下,进入 “d”
  2. 斯普利特 “”
var string = "abcdeabcde"; 
var newstringreplaced = string.replace(/d/gi, ",d"); 
var newstring = newstringreplaced.split(","); 
return newstring; 

希望这有助于。

+0

根据萤火虫探查器的最快速答案 – 2010-12-22 22:15:42

+4

你的解决方案有一个错误 - 当我用“,d”替换不是“d”的实例时,它有效。好主意!接受因为简单:) – 2010-12-22 22:17:12

+11

“abcdeabcde”.split(/(d)/); – Kai 2010-12-22 22:19:30

1

试试这个:

var string = "abcdeabcde"; 
    var delim = "d"; 
    var newstring = string.split(delim); 
    var newArr = []; 
    var len=newstring.length; 
    for(i=0; i<len;i++) 
    { 
     newArr.push(newstring[i]); 
     if(i != len-1)newArr.push(delim); 
    } 
0
function split2(original){ 
    var delimiter = "d", result = [], tmp; 
    tmp = original.split(delimiter); 
    tmp.forEach(function(x){result.push(x); result.push(delimiter); }); 
    return result; 
} 
39

尝试:

"abcdeabcde".split(/(d)/); 
1

只是覆盖String.prototype.split功能与此一:

String.prototype._split = String.prototype.split; 
String.prototype.split = function(delimiter, keepDelimiters) { 
    if (!keepDelimiters) { 
     return this._split(delimiter); 
    } else { 
     var res = []; 
     var start = 0, index; 
     while ((index = this.indexOf(delimiter, start)) != -1) { 
      res.push(this.substr(start, index - start)); 
      res.push(delimiter); 
      start = index + 1; 
     } 
     res.push(this.substr(start)); 
     return res; 
    } 
}; 


var str = "abcdeabcde"; 
alert(str.split("d", true)); 
alert(str.split("d")); 

// output : 
// 
// [ 'abc', 'd', 'eabc', 'd', 'e' ] 
// [ 'abc', 'eabc', 'e' ] 

这种方法不会失败,不使用正则表达式并且是缺点与原来的String.split功能相同。它也符合str.split(/(d)/);解决方案,但在IE中不会失败。

唯一的限制是,如果使用第二个参数(split替换插入函数),则分隔符不能是正则表达式。但是,如果你仔细想想一点,它不是一个真正的限制,因为如果使用正则表达式,你不需要第二个参数...

0

试试这个

var string = "abcdeabcde"; 
var res = string.replace(/d/g, 'd\0').split(/(?=d)|\0/); 
console.log(res); 
//["abc", "d", "eabc", "d", "e"] 
18
var parts= string.split('d'); 
for (var i= parts.length; i-->1;) 
    parts.splice(i, 0, 'd'); 

(转回的循环有必要避免将d s添加到已插入d s的列表中的某些部分。)

5

拆分拆分用于创建不用于搜索的单独行。

[^ d] - 找到不含有一组子的 “d”

var str = "abcdeabcde"; 

str.match(/[^d]+|d/g)   // ["abc", "d", "eabc", "d", "e"] 
or 
str.match(/[^d]+/g)   // ["abc", "eabc", "e"] 
or 
str.match(/[^d]*/g)   // ["abc", "", "eabc", "", "e", ""] 

读 “RegExp对象”,如果你不想用 “的javascript” 的问题。

2

这是我的正则表达式分隔符版本。它与String.prototype.split具有相同的接口;它将对待全球和非全球正则表达式没有区别。返回值是一个数组,它的奇数成员与分隔符相匹配。

function split(text, regex) { 
    var token, index, result = []; 
    while (text !== '') { 
     regex.lastIndex = 0; 
     token = regex.exec(text); 
     if (token === null) { 
      break; 
     } 
     index = token.index; 
     if (token[0].length === 0) { 
      index = 1; 
     } 
     result.push(text.substr(0, index)); 
     result.push(token[0]); 
     index = index + token[0].length; 
     text = text.slice(index); 
    } 
    result.push(text); 
    return result; 
} 

// Tests 
assertEquals(split("abcdeabcde", /d/), ["abc", "d", "eabc", "d", "e"]); 
assertEquals(split("abcdeabcde", /d/g), ["abc", "d", "eabc", "d", "e"]); 
assertEquals(split("1.2,3...4,5", /[,\.]/), ["1", ".", "2", ",", "3", ".", "", ".", "", ".", "4", ",", "5"]); 
assertEquals(split("1.2,3...4,5", /[,\.]+/), ["1", ".", "2", ",", "3", "...", "4", ",", "5"]); 
assertEquals(split("1.2,3...4,5", /[,\.]*/), ["1", "", "", ".", "2", "", "", ",", "3", "", "", "...", "4", "", "", ",", "5", "", ""]); 
assertEquals(split("1.2,3...4,5", /[,\.]/g), ["1", ".", "2", ",", "3", ".", "", ".", "", ".", "4", ",", "5"]); 
assertEquals(split("1.2,3...4,5", /[,\.]+/g), ["1", ".", "2", ",", "3", "...", "4", ",", "5"]); 
assertEquals(split("1.2,3...4,5", /[,\.]*/g), ["1", "", "", ".", "2", "", "", ",", "3", "", "", "...", "4", "", "", ",", "5", "", ""]); 
assertEquals(split("1.2,3...4,5.", /[,\.]/), ["1", ".", "2", ",", "3", ".", "", ".", "", ".", "4", ",", "5", ".", ""]); 
assertEquals(split("1.2,3...4,5.", /[,\.]+/), ["1", ".", "2", ",", "3", "...", "4", ",", "5", ".", ""]); 
assertEquals(split("1.2,3...4,5.", /[,\.]*/), ["1", "", "", ".", "2", "", "", ",", "3", "", "", "...", "4", "", "", ",", "5", "", "", ".", ""]); 
assertEquals(split("1.2,3...4,5.", /[,\.]/g), ["1", ".", "2", ",", "3", ".", "", ".", "", ".", "4", ",", "5", ".", ""]); 
assertEquals(split("1.2,3...4,5.", /[,\.]+/g), ["1", ".", "2", ",", "3", "...", "4", ",", "5", ".", ""]); 
assertEquals(split("1.2,3...4,5.", /[,\.]*/g), ["1", "", "", ".", "2", "", "", ",", "3", "", "", "...", "4", "", "", ",", "5", "", "", ".", ""]); 

// quick and dirty assert check 
function assertEquals(actual, expected) { 
    console.log(JSON.stringify(actual) === JSON.stringify(expected)); 
} 
8

可以在一个行完成:

var string = "abcdeabcde";  
string.split(/(d)/); 
["abc", "d", "eabc", "d", "e"] 
6

我喜欢Kai's answer,但它是不完整的。而是使用:

"abcdeabcde".split(/(?=d)/g) //-> ["abc", "deabc", "de"] 

这是使用正则表达式中的一个Lookahead Zero-Length Assertion,这使得比赛没有捕获组的一部分。没有其他技巧或解决方法需要。

1

尝试此

"abcdeabcde".split("d").reduce((result, value, index) => { 
    return (index !== 0) ? result.concat(["d", value]) : result.concat(value) 
}, [])