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

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

Related

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>

Escape / in .split()

Need to escape / in javascript .split() function but cannot seem to figure out!
input -> string "07777/25555,00255" or any of 0777/2555 0777,2555
output -> array {07777,25555,00255}
var p = item.gdp.split(/ , | \//);
Not really good with regex!
What this does is split on either " , " or " /" (note the space characters: space comma space and space forward slash). Your regular expression is absolutely fine if that's what you're intending to replace on.
Here's a Regexper visualisation:
Update
There are no spaces in your string at all, so you need to remove those:
item.gdp.split(/,|\//);
With this, your result will be:
["07777", "25555", "00255"]
A more practical regular expression to use though would be /[,\/] - the square brackets will match on any character held within them.
var item={gdp:"07777/25555,00255"};
var p = item.gdp.split(/[,/]/);
document.write(p[0] + "<br>" + p[1] + "<br>" + p[2]);
07777
25555
00255
Here's one
split(/\s*[,\/]\s*|\s+/);
If you are splitting on only comma and slash as in your first string
"07777/25555,00255"
you can simply split on the character class containing those two characters [,/]
Within a character class the slash does not need to be escaped, so the resulting statement would be
var p = item.gdp.split(/[,/]/);
If you also want to split on space, as in your other example 0777/2555 0777,2555 simply add space to the character class:
var p = item.gdp.split(/[, /]/);
or to split on any whitespace (space, tab, etc.) use the predefined \s:
var p = item.gdp.split(/[,\s/]/);
Further, you can collapse multiple whitespace, but then you need to go beyond a simple character class. Compare...
var str="07777/25555,00255 0777,3444";
// split on white, comma, or slash. multiple spaces causes multiple results
str.split(/[\s,/]/)
// -> ["07777", "25555", "00255", "", "", "", "", "0777", "3444"]
// split on multiple whitespace, OR on comma or slash
str.split(/\s+|[,/]/)
// -> ["07777", "25555", "00255", "0777", "3444"]
input.split(/[\/\s,]+/)
Is this what you are looking for?

How to split a string with a backslash in javascript?

I have a string containing two backslashes in it:
str = "active - error - oakp-ms-001 Volume Usage-E:\ PercentUsed E:\"
I want to pick up only "oakp-ms-001" from the above string, but as the string contains backslash in it I am not able to split the string.
Please let me know if there is any solution for this?
First, I'll note that the code you've quoted has a syntax error:
str = "active - error - oakp-ms-001 Volume Usage-E:\ PercentUsed E:\"
There's no ending " on that string, becaue the \" at the end is an escaped ", not a backslash followed by an ending quote.
If there were an ending quote on the string, it would have no backslashes in it (the \ with the space after it is an invalid escape that ends up just being a space).
So let's assume something valid rather than a syntax error:
str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\";
That has backslashes in it.
What you need to do doesn't really involve "splitting" at all but if you want to split on something containing a backslash:
var parts = str.split("\\"); // Splits on a single backslash
But I'm not seeing how splitting helps with what you've said you want.
You have to identify what parts of the string near what you want are consistent, and then create something (probably a regular expression with a capture group) that can find the text that varies relative to the text that doesn't.
For instance:
var str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\";
var match = str.match(/error - (.*?) ?Volume/);
if (match) {
console.log(match[1]); // oakp-ms-001
}
There I've assumed the "error - " part and the "Volume" part (possibly with a space in front of it) are consistent, and that you want the text between them.
Live Example
JSON.stringify(fileName).split(“\”);
It’s should be double backslash inside the split
This is a non-terminating string to begin with (you're escaping the closing quotation mark), so I'm going to assume your string looks more like this:
str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\";
If you want to split the string by backslashes and spaces alike, the first step is to split by backslashes, done like this:
step2 = str.split("\\");
Note that you have to escape the backslash here.
The second thing to do is to now split this string by spaces, but because it's an array you have to use a loop to do this:
var step3 = [];
for(var i = 0; i < step2.length; i++){
step3 += step2[i].split(" ");
}
And then you can simply split step3 by "," characters and find the phrase before "Volume". This probably isn't the best answer, but it gets you the data you want.
escape your backslash! \ becomes \\ so in fact you assign like ths:
str = "active - error - oakp-ms-001 Volume Usage-E:\\ PercentUsed E:\\"
This is a solution for this question
str.split(/[\$]/)

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

Javascript - How to remove all extra spacing between words

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

Categories

Resources