Replace all whitespace characters

JavascriptTrim

Javascript Problem Overview


I want to replace all occurrences of white space characters (space, tab, newline) in JavaScript.
How to do so?

I tried:

str.replace(/ /gi, "X")

Javascript Solutions


Solution 1 - Javascript

You want \s

> Matches a single white space > character, including space, tab, form > feed, line feed.

Equivalent to

[ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]

in Firefox and [ \f\n\r\t\v] in IE.


str = str.replace(/\s/g, "X");

Solution 2 - Javascript

We can also use this if we want to change all multiple joined blank spaces with a single character:

str.replace(/\s+/g,'X');

See it in action here: https://regex101.com/r/d9d53G/1

Explanation

> / \s+ / g

  • \s+ matches any whitespace character (equal to [\r\n\t\f\v ])
  • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

  • Global pattern flags
  • g modifier: global. All matches (don't return after first match)

Solution 3 - Javascript

\s is a meta character that covers all white space. You don't need to make it case-insensitive — white space doesn't have case.

str.replace(/\s/g, "X")

Solution 4 - Javascript

Have you tried the \s?

str.replace(/\s/g, "X");

Solution 5 - Javascript

If you use

str.replace(/\s/g, "");

it replaces all whitespaces. For example:

var str = "hello my world";
str.replace(/\s/g, "") //the result will be "hellomyworld"

Solution 6 - Javascript

Try this:

str.replace(/\s/g, "X")

Solution 7 - Javascript

Not /gi but /g

var fname = "My Family File.jpg"
fname = fname.replace(/ /g,"_");
console.log(fname);

gives

"My_Family_File.jpg"

Solution 8 - Javascript

Actually it has been worked but

just try this.

take the value /\s/g into a string variable like

String a = /\s/g;

str = str.replaceAll(a,"X");

Solution 9 - Javascript

You could use the function trim

let str = ' Hello World ';

alert (str.trim());

All the front and back spaces around Hello World would be removed.

Solution 10 - Javascript

I've used the "slugify" method from underscore.string and it worked like a charm:

https://github.com/epeli/underscore.string#slugifystring--string

The cool thing is that you can really just import this method, don't need to import the entire library.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSouravView Question on Stackoverflow
Solution 1 - JavascriptAlex K.View Answer on Stackoverflow
Solution 2 - JavascriptMilos StankovicView Answer on Stackoverflow
Solution 3 - JavascriptQuentinView Answer on Stackoverflow
Solution 4 - JavascriptMichael BerkowskiView Answer on Stackoverflow
Solution 5 - JavascriptGigiProveView Answer on Stackoverflow
Solution 6 - JavascriptHeadshotaView Answer on Stackoverflow
Solution 7 - JavascriptDimitrios VerveridisView Answer on Stackoverflow
Solution 8 - JavascriptSitenView Answer on Stackoverflow
Solution 9 - JavascriptAnseamView Answer on Stackoverflow
Solution 10 - JavascriptdudeView Answer on Stackoverflow