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?
- 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.
- 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
Angularjs - How to Get an Ngrepeat Filtered Result Reference
How to Access Google Sheet Spreadsheets Only with JavaScript
JavaScript 'In' Operator for 'Undefined' Elements in Arrays
How to Access Parent Window Object Using Jquery
How to Replace a Regex Substring Match in JavaScript
Why Are Certain Function Calls Termed "Illegal Invocations" in JavaScript
Password-Check Directive in Angularjs
Getting an Absolute Url from a Relative One. (IE6 Issue)
How to Add a Class to a Dom Element in JavaScript
JavaScript Custom Event Listener
Detect If Browser Tab Is Active or User Has Switched Away
What the Difference Between .Click and .Change on a Checkbox
Aggregation Filter After $Lookup
Appending Array to Formdata and Send via Ajax
How to Set a Cookie with Expire Time
Google Maps Move Marker with Lat/Lng from Ajax Success Returned Data