最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to split string between two separators in javascript? - Stack Overflow

programmeradmin3浏览0评论

I know how to split using, multiple separators but I have no idea how to split a string into an array between two characters. So:

var myArray = "(text1)(text2)(text3)".split(???)
//=> myArray[0] = "text1", myArray[1] = "text2", myArray[2] = "text3"

What should I enter in the "???"? Or is there a different approach I should use?

Making ")(" a separator won't work as I want to split the array with a variety of separators such as ">" making it very unpractical to list every possible combination of separators

I know how to split using, multiple separators but I have no idea how to split a string into an array between two characters. So:

var myArray = "(text1)(text2)(text3)".split(???)
//=> myArray[0] = "text1", myArray[1] = "text2", myArray[2] = "text3"

What should I enter in the "???"? Or is there a different approach I should use?

Making ")(" a separator won't work as I want to split the array with a variety of separators such as ">" making it very unpractical to list every possible combination of separators

Share Improve this question edited Jan 17, 2019 at 5:41 Cœur 38.7k26 gold badges202 silver badges277 bronze badges asked Nov 27, 2012 at 22:22 curious-catcurious-cat 4211 gold badge7 silver badges17 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 13
.split(/[()]+/).filter(function(e) { return e; });

See this demo.

Using split between specific characters without losing any characters is not possible with JavaScript, because you would need a lookbehind for that (which is not supported). But since you seem to want the texts inside the parentheses, instead of splitting you could just match the longest-possible string not containing parentheses:

myArray = "(text1)(text2)(text3)".match(/[^()]+/g)
发布评论

评论列表(0)

  1. 暂无评论