AxelBoden
AxelBoden

Reputation: 23

RegExp / JavaScript: Split string on multiple characters, keep separators without using lookbehind

Let's say I have a string: "This is a string-thing". I want to split on both space and hyphen, but keep the separators together with the previous word => ["This ", "is ", "a ", "string-", "thing"]

I'm currently doing this using the following regExp:

string.split(/(?<=[\s-])/g)

This does what is supposed to.. at least in Chrome. As I understand Safari doesn't support lookbehind in regExp which breaks the code. Is there any way to do this without lookbehind?

Upvotes: 2

Views: 344

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

You might also use split with a capture group to keep the value to split on, and remove the empty entries from the resulting array.

The pattern matches

  • ( Capture group 1
    • [^\s-]* Repeat 0+ times matching any char other than a whitespace char or -
    • [\s-] Match either a whitespace char or -
  • ) Close group 1

Note that \s can also match a newline.

const s = "This is a string-thing";
const regex = /([^\s-]*[\s-])/;
console.log(s.split(regex).filter(Boolean));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386600

You could match the words without lookbehind.

const
    string = "This is a string-thing",
    parts = string.match(/.+?([\s-]|$)/g);

console.log(parts);

Upvotes: 2

Related Questions