Split a string based on multiple delimiters

前端 未结 6 2143
情深已故
情深已故 2020-11-29 06:12

I was trying to split a string based on multiple delimiters by referring How split a string in jquery with multiple strings as separator

Since multiple delimiters I

6条回答
  •  感情败类
    2020-11-29 06:59

    If you want to split based in multiple regexes and dont want to write a big regex You could use replace and split. Like this:

    const spliters = [
    	/(\[products\])/g,
    	/(\[link\])/g,
    	/(\[llinks\])/g,
    ];
    let newString = "aa [products] bb [link] [products] cc [llinks] dd";
    spliters.forEach(regex => {
    	newString = newString.replace(regex, match => `æææ${match}æææ`);
    });
    const mySplit = newString.split(/æææ([^æ]+)æææ/)
    console.log(mySplit);

    This works very well for my case.

提交回复
热议问题