for example i am using :
string.replace(/s+/g, " ").trim(); // here string being my string name
it replaces all extra spaces with a single space along with line breaks as well.
But i want the line breaks to stay as they are while the extra spaces in a line to be replaced with a single space
2
Answers
You could use the following negative character class trick here:
The pattern
[^Sn]
matches:S
, which is equivalent tos
(what you currently have)n
, which spares newlines from being replacedYou could use the following negative character class trick here:
string.replace(/[^Sn]/g, " ").trim();
The pattern [^Sn] matches:
Not S, which is equivalent to s (what you currently have)
But also not n, which spares newlines from being replaced