How to Remove Leading and Trailing White Spaces from a Given HTML String

Javascript remove leading and trailing spaces from multiline string and replace the rest of whitespace chunks with commas

The string conversion might be easier with split/join and trim:

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

The extra credit is a little more involved. :)

const rows = data.split(/\r?\n/).map(row => row.trim().split(/\s+/).join(','));
const keys = rows.shift().split(',');
const chunks = rows.join("\n").split(/\n{2,}/);

const output = chunks .map(chunk => chunk.split("\n").map(
row => row.split(',').reduce((obj, v, i) => {
obj[keys[i]] = v;
return obj;
}, {})
));

How to remove leading and trailing white spaces from input text?

  1. Using trim() method works fine, but is used in newer browsers.
    function removeWhitespaceUsingTrimMethod {

var str = " This is whitespace string for testing purpose ";
var wsr = str.trim();
alert(wsr);
}

Output:
This is whitespace string for testing purpose

From Docs:

(method) String.trim(): string

Removes the leading and trailing white space and line terminator
characters from a string.


  1. Using replace() method – works in all browsers

Syntax:

testStr.replace(rgExp, replaceText);
str.replace(/^\s+|\s+$/g, '');

function removeWhitespaceUsingReplaceMethod {

var str = " This is whitespace string for testing purpose ";
var wsr = str.replace(/^\s+|\s+$/g, '');
alert( wsr);
}

Output:
This is whitespace string for testing purpose

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

Remove trailing and leading white spaces around delimiter using regex

You could use /\s*,\s*/g, and then .trim() the string.

RegEx to Remove Trailing Space

The simplest option would be to chain the .trim() method before replacing the whitespace. In doing so, the trailing whitespace is removed.

string.toLowerCase().trim().replace(/\s+/g, '-')

It would output my-test-string:

var text = 'My (test) string ';
var id = text.toLowerCase().trim().replace(/\s+/g, '-')
.replace(/[^a-zA-Z0-9-]/g, '');

console.log(id); // my-test-string

Of course, you could alternatively just use a negative lookahead in order to prevent the whitespace at the end from being replaced:

string.toLowerCase().replace(/\s+(?!\s*$)/g, '-')

JavaScript remove leading and trailing spaces

try doing

trimmedstr = str.replace(/\s+$/, '');

or maybe

.replace(/ +$/, "");

Remove leading and trailing whitespaces from string in Vim

My attempt:

func! RemoveWhitespaces(string)
let string = a:string
let result = substitute(string, '\s\+$', '', '')
let result = substitute(string, '^\s\+', '', '')
silent return result
endfunc

Now to get your result you can:

:let var=RemoveWhitespaces('  my string with spaces   ')
:echo var

update:

As romainl commentted, there is a easier way:

:let var=trim('   some text   ')


Related Topics



Leave a reply



Submit