Javascript - How to remove all extra spacing between words - javascript

How can I remove all extra space between words in a string literal?
"some value"
Should become
"some value"
Also,
" This should become something else too . "
Becomes
"This should become something else too ."
Do not worry about moving the .. Just as above is fine. I know I can use $.trim(str) to achieve the trailing/ending space removal. But, I'm not sure how to do the 1 space between words trick.

var string = " This should become something else too . ";
string = string.replace(/\s+/g, " ");
This code replaces a consecutive set of whitespace characters (\s+) by a single white space. Note that a white-space character also includes tab and newlines. Replace \s by a space if you only want to replace spaces.
If you also want to remove the whitespace at the beginning and end, include:
string = string.replace(/^\s+|\s+$/g, "");
This line removes all white-space characters at the beginning (^) and end ($). The g at the end of the RegExp means: global, ie match and replace all occurences.

var str = " This should become something else too . ";
str = str.replace(/ +(?= )/g,'');
Here's a working fiddle.

In case we want to avoid the replace function with regex,
We can achieve same result by
str.split(' ').filter(s => s).join(' ')
// var str = " This should become something else too . ";
// result is "This should become something else too ."
First, split the original string with space, then we will have empty string and words in an array. Second, filter to remain only words, then join all words with a whitespace.

var str = " This should become something else too . "
$.trim(str).replace(/\s(?=\s)/g,'')
This uses lookahead to replace multiple spaces with a single space.

jsFiddle Example
" This should become something else too . ".replace(/[\s\t]+/g,' ');

Another (perhaps easier to understand) regexp replacement that will do the trick:
var input = /* whatever */;
input = input.replace(/ +/g, ' ');
The regexp matches one or more spaces, so the .replace() call replaces every single or repeated space with a single space.

var str = 'some value';
str.replace(/\s\s+/g, ' ');

Related

How to replace all \n with space? [duplicate]

I have a var that contains a big list of words (millions) in this format:
var words = "
car
house
home
computer
go
went
";
I want to make a function that will replace the newline between each word with space.
So the results would something look like this:
car house home computer go went
You can use the .replace() function:
words = words.replace(/\n/g, " ");
Note that you need the g flag on the regular expression to get replace to replace all the newlines with a space rather than just the first one.
Also, note that you have to assign the result of the .replace() to a variable because it returns a new string. It does not modify the existing string. Strings in Javascript are immutable (they aren't directly modified) so any modification operation on a string like .slice(), .concat(), .replace(), etc... returns a new string.
let words = "a\nb\nc\nd\ne";
console.log("Before:");
console.log(words);
words = words.replace(/\n/g, " ");
console.log("After:");
console.log(words);
In case there are multiple line breaks (newline symbols) and if there can be both \r or \n, and you need to replace all subsequent linebreaks with one space, use
var new_words = words.replace(/[\r\n]+/g," ");
See regex demo
To match all Unicode line break characters and replace/remove them, add \x0B\x0C\u0085\u2028\u2029 to the above regex:
/[\r\n\x0B\x0C\u0085\u2028\u2029]+/g
The /[\r\n\x0B\x0C\u0085\u2028\u2029]+/g means:
[ - start of a positive character class matching any single char defined inside it:
\r - (\x0D) - \n] - a carriage return (CR)
\n - (\x0A) - a line feed character (LF)
\x0B - a line tabulation (LT)
\x0C - form feed (FF)
\u0085 - next line (NEL)
\u2028 - line separator (LS)
\u2029 - paragraph separator (PS)
] - end of the character class
+ - a quantifier that makes the regex engine match the previous atom (the character class here) one or more times (consecutive linebreaks are matched)
/g - find and replace all occurrences in the provided string.
var words = "car\r\n\r\nhouse\nhome\rcomputer\ngo\n\nwent";
document.body.innerHTML = "<pre>OLD:\n" + words + "</pre>";
var new_words = words.replace(/[\r\n\x0B\x0C\u0085\u2028\u2029]+/g," ");
document.body.innerHTML += "<pre>NEW:\n" + new_words + "</pre>";
Code : (FIXED)
var new_words = words.replace(/\n/g," ");
Some simple solution would look like
words.replace(/(\n)/g," ");
No need for global regex, use replaceAll instead of replace
myString.replaceAll('\n', ' ')

Javascript regex, make remove single paragraph line breaks

I've got text in this format:
word word,
word word.
word word
word word.
Not specific to that two word format, it's just a line break before so many characters, rather than one long string of paragraph. But I'm trying to get it to be that one long string of paragraph. So it should look like this:
word word, word word.
word word word word.
If I use the code text.replace(/$\n(?=.)/gm, " ") and output that to the terminal I get text that looks like:
word word, word word.
word word word word.
It's got an extra space at the start of the paragraph, but that's good enough for what I'm trying to do (although if there's also a way to remove it in one replace function than that's good). The problem is that when I output it to a textarea it doesn't remove the \n character, and I just get text that looks like this:
word word,
word word.
word word
word word.
I'm trying to do this all client side, currently running it in Firefox.
I'm not the best with regex, so this might be really simple and I'm just ignorant on how to do it. But any help would be really appreciated. Thanks!
A carriage return is \r so you would need to use
text.replace(/$(\r|\n)(?=.)/gm, " ");
Below a snippet of code that satisfy your request, i've removed the leading whitespaces too (caused by empty lines), using a closure with the replace function:
var regex = /([^.])\s+/g;
var input = 'word word,\nword word.\n\nword word\nword word.';
var result = input.replace(regex, function(all, char) {
return (char.match(/\s/)) ? char : char + ' ' ;
});
document.write('<b>INPUT</b> <xmp>' + input + '</xmp>');
document.write('<b>OUTPUT</b> <xmp>' + result + '</xmp>');
Regex Breakout
([^.]) # Select any char that is not a literal dot '.'
# and save it in group $1
\s+ # 1 or more whitespace char, remove trailing spaces (tabs too)
# and all type of newlines (\r\n, \r, \n)
NOTE
if for some reason you want to keep the leading whitespace, simplify the code below as follow:
var regex = /([^.])\s+/g;
var replace = '$1 ';
var input = 'word word,\nword word.\n\nword word\nword word.';
var result = input.replace(regex, replace);
document.write('<b>INPUT</b> <xmp>' + input + '</xmp>');
document.write('<b>OUTPUT</b> <xmp>' + result + '</xmp>');
You probably missed some \r, here's a way to match all sort of new lines and not have extra spaces:
var input = 'word word,\nword word.\n\nword word\nword word.';
// split if 2 or more new lines
var out = input.split(/(\r\n|\n|\r){2,}?/)
// split the paragraph by new lines and join the lines by a space
.map((v) => v.split(/\r\n|\n|\r/).join(' '))
// there is some spaces hanging in the array, filter them
.filter((v) => v.trim())
// join together all paragraphs by \n
.join('\n');
$('#txt').append(out);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="txt"></textarea>

Remove empty values from comma separated string javascript

How do I remove empty values from an comma separated string in JavaScript/jQuery?
Is there a straightforward way, or do I need to loop through it and remove them manually?
Is there a way to merge all the splits (str and str1) in JavaScript/jQuery?
CODE:
var str = '+ a + "|" + b';
var str1 = '+ a + "-" + b';
str = str.split("+").join(",").split('"|"').join(",");
str1 = str1.split("+").join(",").split('"-"').join(",");
console.log(str); //, a , , , b
console.log(str1); //, a , , , b
EXPECTED OUTPUT :
a,b
Help would be appreciated :)
As I see it, you want to remove +, "|", "-" and whitespace from the beginning and end of the string, and want to replace those within the string with a single comma. Here's three regexes to do that:
str = str.replace(/^(?:[\s+]|"[|-]")+/, '')
.replace(/(?:[\s+]|"[|-]")+$/, '')
.replace(/(?:[\s+]|"[|-]")+/g, ',');
The (?:[\s+]|"[|-]") matches whitespace or pluses, or "|" or "-". The + at the end repeats it one or more times. In the first expression we anchor the match to the beginning of the string and replace it with nothing (i.e. remove it). In the second expression we anchor the match to the end of the string and remove it. And in the third, there is no anchor, because all matches that are left have to be somewhere inside the string - and we replace those with ,. Note the g modifier for the last expression - without it only the first match would be replaced.
The other answer is useful, and may be exactly what you are looking for.
If, for some reason, you still want to use split, luckily that method takes a regex as separator, too:
str = str.split(/\s*\+\s*(?:"\|"\s*\+\s*)?/).slice(1).join(",");
str1 = str1.split(/\s*\+\s*(?:"-"\s*\+\s*)?/).slice(1).join(",");
Because you have a plus sign in front of the "a", you can slice the array to return only the elements after it.
Also, since you mentioned you were new to regular expressions, here is the explanation:
any amount of space
a plus sign
any amount of space
optional (because of the ? after the group, which is the parentheses): a non-capturing (that is what the ?: means) group containing:
"|"
any amount of space
another plus sign
any amount of space
Works perfectly fine:
str.split(/[ ,]+/).filter(function(v){return v!==''}).join(',')

Preserve non-break space &nbsp in jQuery trim function

I want to trim only regular whitespace and preserve &nbsp non-break spaces. What is my best bet?
Update: By trim, I mean either leading or trailing. I specifically need to eliminate leading whitespace other than &nbsp, but I will appreciate answers that do the same for also trailing or just trailing.
If the string is the content of an HTML tag you can cheat a bit, using:
var result = $( '#element' ).html().trim();
which will not trim because it will literally be in the string.
If the string is not in a tag you could try:
var result = $( '<p>' + the_string + '</p>' ).html().trim();
which should do the same thing.
Replace all white spaces with blank.
var dest = 'test value';
result_val=dest.split(" ").join("");
demo
If you want to remove empty characters in the middle of the string, you can use this (note the first parameter means any amount of whitespace but shouldn't match an ):
var newString = s.replace(/\s+/g, '');
If not (meaning only remove white space at the start/end of the string), you can use
var newString = s.trim();

Help with regex in javascript

Whatever string is given I have to see if there is exactly one space after and before =, If it is more than one space in either side I have to reduce that to one and if there is none, I have to insert one.
How should I do that ? String can contain anything.
Thanks
You can do this:
str = str.replace(/ *= */g, " = ");
This will replace all = characters regardless of how many spaces it is surrounded by. The * quantifier will match as most spaces as possible while allowing even no spaces at all.
Try this:
var out = in.replace(/ *= */g, " = ");
Basically just replace zero or more instances of a space with a space and you get both desired results. If zero, then you get one. If more than one, you get one.
Make the following replacement:
s = s.replace(/ *= */g, ' = ')
myString.replace(/\s*=\s*/g, " = ")
will do the same as other given answers, but allow any type of space characters to be replaced (spaces, tabs, etc.).

Categories

Resources