-3

Given the following sample input:

rgb(97, 0, 255) none repeat scroll 0% 0% / auto padding-box border-box

I'm trying to split this string right after the closing bracket with the following regex:

string.split("\\)\\s")[0]

Problem is that this approach is chopping off the bracket:

rgb(97, 0, 255

How can I get the following output?

rgb(97, 0, 255)

How do I split without chopping off the closing bracket?

lemon
  • 14,875
  • 6
  • 18
  • 38
ken4ward
  • 2,246
  • 5
  • 49
  • 89
  • @Jens zero-width matches will not be removed. Or rather, they would but that removes the empty string. You can still split based on some character and still keep it. – VLAZ Aug 03 '22 at 12:49
  • Why are you using `split` at all? Why not just `s.substring(0, s.indexOf(")") + 1)`? Note: parsing CSS is a lot more complicated than just splitting a string. – VGR Aug 03 '22 at 18:31

1 Answers1

2

You may need a Positive Lookbehind, which will look for what's before your match, yet without matching it:

(?<=\))\s

Check the demo here.

lemon
  • 14,875
  • 6
  • 18
  • 38