0

I am having a trouble with removing all spaces from a string.

Stehn Stehn Simchart Simchart Stehn Stehn Stehn Stehn Stehn 

This is the string, which has new lines, spaces, and so on.

string.replace(/[\r\n]+/gm, ', ') 

Doing this, only returns me

Stehn , Stehn , Simchart, Simchart, Stehn , Stehn , Orion Reederei GmbH & Co KG, Orion Reederei GmbH & Co KG, Stehn , Stehn , Stehn , Arrow Shipping Germany, "

But I would like to get a normalized and sanitized string with commas.

string.replace(/\r?\n|\r/g, ',').split(',').map(s => s.trim()).join(', ') 

This code, however, resolves my problem, but I don't like this solution as I have thousands inside my table.

9
  • 1
    \s matches any whitespace character - is that what you need? Commented May 23, 2020 at 7:37
  • Its \s , but it doesn't help me Commented May 23, 2020 at 7:38
  • \s matches \r,\n and \s, i.e whitespaces. So i'd prefer if you gave a literal whitespace if that's what you are targeting Commented May 23, 2020 at 7:39
  • Why doesn't \s help? You want all whitespace to be replaced so it seems like the logical choice. If it doesn't work, then there is something that isn't present in the question. Commented May 23, 2020 at 7:43
  • Yes I know, We can replace it, but I tried different ways. And also a problem is that I have a word like Orion Reederei GmbH & Co KG and it has spaces but not replaceable. Commented May 23, 2020 at 7:44

5 Answers 5

2

We can just try replacing all whitespace along with an immediately following CR?LF with just a single comma and space.

var input = "Stehn \nStehn \nSimchart\nSimchart\nStehn \nStehn \nStehn \nStehn \nStehn "; var output = input.replace(/\s*?\r?\n/g, ", "); console.log(output);

Sign up to request clarification or add additional context in comments.

1 Comment

@NorayrGhukasyan If that works, use a more efficient /[^\S\n]*\n/g
2

You could use a single pattern, and in the replacement use a comma and a single space.

[^\S\r\n]*[\r\n]+ 
  • [^\S\r\n]* Negated character class, match 0+ times a whitespace char except newlines
  • [\r\n]+ Match 1+ newlines (for a single newline, use \r?\n)

Regex demo

 const regex = /[^\S\r\n]*[\r\n]+/g; const str = `Stehn Stehn Simchart Simchart Stehn Stehn Stehn Stehn Stehn `; const result = str.replace(regex, `, `); console.log(result);

2 Comments

What's better in this case, I am confused. @TimBiegeleisen solution is also good and short. Why the one could be better?
@NorayrGhukasyan Both solutions work. The difference is in that \s can also match \r and \n Making it lazy will involve backtracking to expand the match for \s by one item until it matches a newline. Using [^\S\r\n] excludes matching newlines.
1

If your string is:

let s = "a b c d e \nf g h\n\ni"; 

Then you can strip with:

s.split(/\s+/) 

And combine:

s.split(/\s+/).join(','); 

If instead it's a newline thing and you want to trim, then why not just split? Easier than replacing, splitting, trimming, and rejoining:

s.split(/\r?\n|\r/).map(s => s.trim()).join(', ') 

2 Comments

Seems s.split(/\r?\n|\r/).map(s => s.trim()).join(', ') is the only solution for this case . I will mark it as accepted, if there is not better solution.
There is a better solution which doesn't require using join.
0

Since you want to substitute whitespace with , you must write a regex that matches whitespaces

Regex: /\b\s+\b/gm

g: global mode

m: multiline mode

Now at this point your regex should match all the spaces. Now all you have to do is replace them with ,

Note that I am using word boundaries here since you do not want , at the end of your last string.

Comments

0

Try this.

function removeAllBlankSpaces(str) { return str.split('').filter(s => s != ' ').join(''); } console.log(removeAllBlankSpaces('str ing'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.