2010-12-22 214 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) 
}, [])