current position:Home>Tips for using JavaScript string split (how to keep the delimiter)

Tips for using JavaScript string split (how to keep the delimiter)

2022-05-15 02:06:04thankseveryday

//  Do not keep delimiters 
"A、B、C".split("、") == ["1", "2", "3"]
//  Keep the delimiter 
"A、B、C".split(/(、)/g) == ["1", "、", "2", "、", "3"]
"A、B、C".split(/(?=、)/g) == ["1", "、2", "、3"]
"A、B、C".split(/(?!、)/g) == ["1、", "2、", "3"]
"A、B、C".split(/(.*?、)/g) == ["", "1、", "", "2、", "3"]
// Split a path, but keep the slashes that follow directories
var str = 'javascript/string/reg.js';
var tokens = str.match(/[^\/]+\/?|\//g);

copyright notice
author[thankseveryday],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205120527258826.html

Random recommended