Replace a space via regex?


#1

I am trying to replace text with spaces so that no spaces remain.

Here is the sample text:

 Content: “blah blah blah.”

The following script produces the following output

const findRegex = /\sContent:\s/g;
const replaceWith = "";

draft.content = draft.content.replace(findRegex, replaceWith);
draft.update();

Output:

 “blah blah blah.”

Is there a way to produce output that does not start with a space? I thought the two ‘\s’ flags in the regex would do that.


#2

That should remove the space between Content: and ”blah” but only finds the first space prior to Content: - if you want to find all spaces leading up to it, add a + qualifier, like:

/\s+Content:\s/g

(You might want it on the \s after, too, if there could be more than one space to strip there, too)


#3

Well, that pattern deleted all the text on the line. This works though:

/\ \ Content:\s/g

Thanks for the hint! I forgot there were two spaces there