How to Remove Spaces from a String Using JavaScript

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, '') );

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,'')

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 space in the middle of a string with $.trim?

You can use regular expression to replace all consecutive spaces \s\s+ with a single space as string ' ', this will eliminate the spaces and keep only one space, then the $.trim will take care of the starting and/or ending spaces:

var string = "hello,           how are you?       ";
console.log($.trim(string.replace(/\s\s+/g, ' ')));

Remove white space between the string using javascript

This should do the trick:

var str = "PB 10 CV 2662";
str = str.replace(/ +/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 excess whitespace from a string by jQuery and only count the actual number of characters?

You can use .trim() as in if ($(this).html().trim().length > len) {

Demo

$(function() {
let len = 70
$('.demo').each(function() {
if ($(this).html().trim().length > len) {
var str = $(this).html().substring(0, len - 1) + "<a class='more'>...顯示更多</a>";
$(this).html(str);
}
});
});
.content {
display: flex;
justify-content: space-between;
border: 1px solid #222;
padding: 10px;
}

.demo {
overflow: hidden;
text-overflow: clip;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}

.photo {
width: 100px;
height: 100px;
border: 1px;
border-radius: 16px;
margin-left: 10px;
}

.more {
text-decoration: none;
color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<div class="demo">
Lorem ipsum dolor sit amet consectetur Lore

</div>
<img class="photo" src="https://images.unsplash.com/photo-1622734659223-f995527e6c7b?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=668&q=80" alt="Sample Image">
</div>


Related Topics



Leave a reply



Submit