underscore.string icon indicating copy to clipboard operation
underscore.string copied to clipboard

New function 'around()' to surround a substring with a string

Open bbottema opened this issue 9 years ago • 0 comments

surround itself is limited, as I would like to add something arround a substring, not the entire string.

Example:

around("1234x6789", 5, '>>', '<<'); // -> "1234>>x<<6789"
around("1234x6789", 4, 6, '>>', '<<'); // -> "123>>4x6<<789"
around("This is an apple", 8, 10, '[', ']'); // -> "This is [an] apple"
around("This is an apple", 'apple', '[', ']'); // -> "This is an [apple]"
around("This is pretty awesome!", /pre(tt)y/, ':', ':'); // -> "This is pre:tt:y awesome!"

Or something along these lines...

Right now I can only do it by manually doing 2 slices or splices.

bbottema avatar May 10 '15 21:05 bbottema