I need to split a string for each d
chracter, which has an optional comma in front.
d(ii)d(ii)
d(ii),d(ii)
dd
d,d
should result in two element arrays:
d(ii) | d(ii)
d(ii) | d(ii)
d | d
d | d
Also a single d
string should result in an array with this single element.
I've started with string.split(',')
but obviously it doesn't take care about the comma
您可以使用d
的正向超前和前面的可选逗号进行拆分。
console.log(['d(ii)d(ii)', 'd(ii),d(ii)', 'dd', 'd,d'].map(s => s.split(/,?(?=d)/)));
function splitD(str){ var result = str.replace(/,/g, "").replace(/d/g, " d").trim().split(" "); console.log(result); return result; } splitD("d(ii)d(ii)"); splitD("d(ii),d(ii)"); splitD("dd"); splitD("d,d");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.