JS string.split()没有删除分隔符 [英] JS string.split() without removing the delimiters
本文介绍了JS string.split()没有删除分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不删除分隔符的情况下拆分字符串?
How can I split a string without removing the delimiters?
假设我有一个字符串:
var string = abcdeabcde;
Let's say I have a string:
var string = "abcdeabcde";
当我做
var newstring = string.split(d)
,我得到这样的结果:
When I do
var newstring = string.split("d")
, I get something like this:
[abc,eabc,e]
但我希望得到这个:
[abc ,d,eabc,d,e]
当我尝试执行split2功能时,我把所有纠结在splice()和索引以及thisvsthat和...... aargh!救命! :D
When I tried to do my "split2" function, I got all entangled in splice() and indexes and "this" vs "that" and ... aargh! Help! :D
推荐答案
试试这个:
- 将所有d实例替换为,d
- 拆分,
var string = "abcdeabcde";
var newstringreplaced = string.replace(/d/gi, ",d");
var newstring = newstringreplaced.split(",");
return newstring;
希望这有帮助。
这篇关于JS string.split()没有删除分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文