简体   繁体   中英

Split string on matching patterns using regular expressions

I am trying to split a string by matching some patterns using regex, for instance i have <span>Hello World</span> and the result would be ["<span>", "Hello World", "</span>"]

// Tried this
console.log(arr.split(/(<*>)/));
// and this:
console.log(arr.split(/(^<$>)/));

You can do something like

 const s = `<span>Hello World</span>`; const output = s.split(/(<\\/?span>)/g).filter(Boolean); console.log(output);

 const a = `<span>Hello World</span>`; var c = a.split(/^(<.*>)(.*?)(<.*?>)$/g).filter(x => x); console.log(c);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM