Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  74] [ 3]  / answers: 1 / hits: 71582  / 14 Years ago, wed, december 22, 2010, 12:00:00

How can I split a string without removing the delimiters?



Let's say I have a string:
var string = abcdeabcde;



When I do
var newstring = string.split(d), I get something like this:



[abc,eabc,e]



But I want to get this:



[abc,d,eabc,d,e]



When I tried to do my split2 function, I got all entangled in splice() and indexes and this vs that and ... aargh! Help! :D


More From » split

 Answers
22

Try this:




  1. Replace all of the d instances into ,d

  2. Split by ,





var string = abcdeabcde;
var newstringreplaced = string.replace(/d/gi, ,d);
var newstring = newstringreplaced.split(,);
return newstring;


Hope this helps.


[#94515] Monday, December 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dakotahs

Total Points: 605
Total Questions: 104
Total Answers: 113

Location: Hungary
Member since Wed, Nov 9, 2022
2 Years ago
;