Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split this string easily

I have the following string:

"<--something--><++something++><**something**>"

The string can have an arbitrary number of "somethings" , even just once.

I need to split it like so:

["<--something-->", "<++something++>", ...]

But I don't know how to proceed best.

I would do something like string.split("><") but then I would have:

["<--something--", "++something++", ...]

And with string.split(/(><)/) I'd get:

["<--something--", "><", "++something++", "><", ...]

I can think of a few less-than-optimal solutions, but I want really elegant one.

like image 793
MarioDS Avatar asked Jan 28 '26 02:01

MarioDS


1 Answers

You're not splitting the string, you are matching it.

Try this:

string.match(/<(.)\1[^>]+?\1\1>/g)

This will match <, two of a kind, then find the same two of a kind and > at the end.

like image 84
Niet the Dark Absol Avatar answered Jan 30 '26 16:01

Niet the Dark Absol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!