首页 > JS获取一个字符串中被指定的两个字符串包括起来的所有字符串数组

JS获取一个字符串中被指定的两个字符串包括起来的所有字符串数组

 

let getStrinsBetweenTwoStrings = (targetString, beginString, endString) => {let aa = targetString.split(beginString), re = [];for (let i = 1, len = aa.length; i < len; i++) {let a = aa[i],s = a.split(endString)[0];s && re.push(s);}return re;
};//测试----------------------------------------
console.log(getStrinsBetweenTwoStrings("一[句话]里面[有]很[多个]方[括号]", "[", "]"));
//[ '句话', '有', '多个', '括号' ]console.log(getStrinsBetweenTwoStrings("我◆◆和我❤❤❤的祖国,◆◆一刻❤❤❤也◆◆不能❤❤❤◆◆分割❤❤❤,无论我走到哪里,都流出◆◆一首赞歌❤❤❤", "◆", "❤❤"));
//[ '和我', '一刻', '不能', '分割', '一首赞歌' ]

更多相关:

  • /*清空控制台信息*/function clearConsole() {console.clear();let fontFamily = "微软雅黑";let fontSize = 40;let fontWeight = "bold";let backgroundColor = "green";console.log("%c有...

  •       现在时刻