I want to trim a string to include 20 words at most using RegEx.
Here’s the text I’m trimming:
const text = `Space travel is the ultimate adventure! Imagine soaring past the stars
and exploring new worlds. It's the stuff of dreams and science fiction,
but believe it or not, space travel is a real thing. Humans and robots
are constantly venturing out into the cosmos to uncover its secrets and
push the boundaries of what's possible`;
Here’s the RegEx pattern:
const regex = new RegExp(`^(\S+\s+){1,20}`);
When I do text.match(regex)
I’m expecting to get this:
Space travel is the ultimate adventure! Imagine soaring past the stars
and exploring new worlds. It’s the stuff of dreams
…But what I’m getting instead is this:
Space travel is the ultimate adventure! Imagine soaring past the stars
and exploring new worlds. It’s the stuff of dreams dreams
I noticed that when I do regex.exec(text)
I’m getting an array with 2 elements: the expected text and that “dreams“.
Setting g flag resolves the issue and removes the “dreams” from the end of the line, but what I don’t understand is why it even appears?
Another curious observation: setting (\S+\s+)
as non-capturing also resolves the problem:
const regex = new RegExp(`^(?:\S+\s+){1,20}`);
Does anyone know why this is happening?
Thanks!