Remove whitespaces inside a string in javascript
For space-character removal use
"hello world".replace(/\s/g, "");
for all white space use the suggestion by Rocket in the comments below!
Remove ALL white spaces from text
You have to tell replace() to repeat the regex:
.replace(/ /g,'')
The g character makes it a "global" match, meaning it repeats the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here.
If you want to match all whitespace, and not just the literal space character, use \s
instead:
.replace(/\s/g,'')
You can also use .replaceAll
if you're using a sufficiently recent version of JavaScript, but there's not really any reason to for your specific use case, since catching all whitespace requires a regex, and when using a regex with .replaceAll
, it must be global, so you just end up with extra typing:
.replaceAll(/\s/g,'')
JS: How to remove white spaces only from inside of string
You may match and capture the leading/trailing whitespaces and restore in the result with the backreferences, and remove all other whitespaces.
var str = " some text ";str = str.replace(/(^\s+|\s+$)|\s+/g, '$1');console.log("'",str,"'");
How to remove spaces from a string using JavaScript?
This?
str = str.replace(/\s/g, '');
Example
var str = '/var/www/site/Brand new document.docx';
document.write( str.replace(/\s/g, '') );
How to remove the extra spaces in a string?
You're close.
Remember that replace
replaces the found text with the second argument. So:
newString = string.replace(/\s+/g,''); // "thiscontainsspaces"
Finds any number of sequential spaces and removes them. Try replacing them with a single space instead!
newString = string.replace(/\s+/g,' ').trim();
Remove all white space from string JavaScript
Use this:
str.replace(/\s+/g, '');
Instead of this:
str.trim()
How to remove the white space at the start of the string
This is what you want:
function ltrim(str) {
if(!str) return str;
return str.replace(/^\s+/g, '');
}
Also for ordinary trim in IE8+:
function trimStr(str) {
if(!str) return str;
return str.replace(/^\s+|\s+$/g, '');
}
And for trimming the right side:
function rtrim(str) {
if(!str) return str;
return str.replace(/\s+$/g, '');
}
Or as polyfill:
// for IE8
if (!String.prototype.trim)
{
String.prototype.trim = function ()
{
// return this.replace(/^\s+|\s+$/g, '');
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
}
if (!String.prototype.trimStart)
{
String.prototype.trimStart = function ()
{
// return this.replace(/^\s+/g, '');
return this.replace(/^[\s\uFEFF\xA0]+/g, '');
};
}
if (!String.prototype.trimEnd)
{
String.prototype.trimEnd = function ()
{
// return this.replace(/\s+$/g, '');
return this.replace(/[\s\uFEFF\xA0]+$/g, '');
};
}
Note:
\s: includes spaces, tabs \t, newlines \n and few other rare characters, such as \v, \f and \r.
\uFEFF: Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF)
\xA0: ASCII 0xA0 (160: non-breaking space) is not recognised as a space character
How to remove whitespace from a string in typescript?
Problem
The trim() method removes whitespace from both sides of a string.
Source
Solution
You can use a Javascript replace method to remove white space like
"hello world".replace(/\s/g, "");
Example
var out = "hello world".replace(/\s/g, "");console.log(out);
Related Topics
Accessing Redux State in an Action Creator
Getting a Better Understanding of Callback Functions in JavaScript
How to Implement Routereusestrategy Shoulddetach for Specific Routes in Angular 2
What's the Fastest Way to Loop Through an Array in JavaScript
What Does [].Foreach.Call() Do in JavaScript
How to Use Multiple Refs for an Array of Elements with Hooks
Sending Emails with JavaScript
What Does Curly Brackets in the 'Var { ... } = ...' Statements Do
Detect Back Button Click in Browser
Why Does (0 < 5 < 3) Return True
How to Close a Browser Window Without Receiving the "Do You Want to Close This Window" Prompt
Populate One Dropdown List Based on the Selection of Other Dropdown List
How to Get HTML Elements from an Object Tag
How to Exchange Variables Between Two HTML Pages
Programmatically Play Video with Sound on Safari and Mobile Chrome
Why My Show Hide Button Needs Double-Click on First Time