2

In the past, I had this regex:

\{(.*?)\}

And entered this string:

logs/{thing:hi}/{thing:hello}

Then I used the following:

console.log(string.split(regex).filter((_, i) => i % 2 === 1));

To get this result:

thing:hi
thing:hello

For irrelevant design reasons, I changed my regex to:

\{.*?\}

But now, when using the same test string and split command, it returns only this:

/

I want it to return this:

{thing:hi}
{thing:hello}

How can I modify the split (or anything else) to do this?

1
  • 1
    Why not match? s.match(/{.*?}/g) Commented Jun 29, 2018 at 0:18

1 Answer 1

3

Why not use match?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

The match() method retrieves the matches when matching a string against a regular expression.

If you're only interested in returning the two string matches then it's much simpler than using split.

var foo = "logs/{thing:hi}/{thing:hello}";
console.log(foo.match(/{.*?}/g));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.