Capture full delimiter with regex

2019-08-25 11:46发布

I've been attempting to learn enough RegEx today to split the following string into the format below:

Original string: foo u:james h c:user p:product

Desired output: ["foo", "u:", "james h", "c:", "user", "p:", "product"]

Actual output: ["foo ", "u", "james h ", "c", "user ", "p", "product"]

The actual output is just missing the colon. It also has trailing spaces, but I can fix that with c# if I can get the full delimiter.

It feels like I'm very close, but I'll confess that the RegEx above required most of my evening. What am I missing?

Here's my RegEx so far:

([a-z]):+

标签: regex
1条回答
【Aperson】
2楼-- · 2019-08-25 12:23

If it's in javascript, you can use the split method, and capture the split pattern that you'd like to keep:

var s = 'foo u:james h c:user p:product'

console.log(
  s.split(/\s+([a-z]:+)/)
)

查看更多
登录 后发表回答