Replace all whitespace characters - javascript

I want to replace all occurrences of white space characters (space, tab, newline) in JavaScript.
How to do so?
I tried:
str.replace(/ /gi, "X")

You want \s
Matches a single white space
character, including space, tab, form
feed, line feed.
Equivalent to
[ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]
in Firefox and [ \f\n\r\t\v] in IE.
str = str.replace(/\s/g, "X");

We can also use this if we want to change all multiple joined blank spaces with a single character:
str.replace(/\s+/g,'X');
See it in action here: https://regex101.com/r/d9d53G/1
Explanation
/ \s+ / g
\s+ matches any whitespace character (equal to [\r\n\t\f\v ])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)

\s is a meta character that covers all white space. You don't need to make it case-insensitive — white space doesn't have case.
str.replace(/\s/g, "X")

Have you tried the \s?
str.replace(/\s/g, "X");

If you use
str.replace(/\s/g, "");
it replaces all whitespaces. For example:
var str = "hello my world";
str.replace(/\s/g, "") //the result will be "hellomyworld"

Try this:
str.replace(/\s/g, "X")

Not /gi but /g
var fname = "My Family File.jpg"
fname = fname.replace(/ /g,"_");
console.log(fname);
gives
"My_Family_File.jpg"

You could use the function trim
let str = ' Hello World ';
alert (str.trim());
All the front and back spaces around Hello World would be removed.

Actually it has been worked but
just try this.
take the value /\s/g into a string variable like
String a = /\s/g;
str = str.replaceAll(a,"X");

I've used the "slugify" method from underscore.string and it worked like a charm:
https://github.com/epeli/underscore.string#slugifystring--string
The cool thing is that you can really just import this method, don't need to import the entire library.

Related

JS\TS - unable to add a white space in a string based on multiple special characters

I'm looking for a smart way to add white space after special character in a long string.
let str = "this\is\an\example\for\a\long\string";
str = str.split("\\").join("\\ ");
This would produce:
"this\ is\ an\ example\ for\ a\ long\ string";
I am looking for something more generic to capture multiple special chars at once, something like this:
let str = "this.is.a\long-mixed.string\with\many.special/characters";
str = str.split(/[.\-_]/).join(/[. \- _ ]/); //note the white spaces after the dot, hyphen and slash. I need to cover as much special chars as possible.
EDIT
I need this to support multi languages. So basically English\Arabic\Hebrew words should not be whitespaced, But only insert a whitespace after a special char.
You can do it like this
So here with replace i am matching anything except alphabets and digits. and than simply adding a space to it.
let str = "this.is.a\long-mixed.string\with\many.special/characters";
str = str.replace(/([\W_])/g, "$1 ");
console.log(str);
([\W_]) - Matches anything except alphabets and digits.

Match parts of code

I'm trying to match parts of code with regex. How can I match var, a, =, 2 and ; from
"var a = 2;"
?
I believe you want this regexp: /\S+/g
To break it down: \S selects all non-whitespace characters, + makes sure you it selects multiple non whitespace characters together (i.e. 'var'),
and the 'g' flag makes sure it selects all of the occurrences in the string, and instead of stopping at the first one which is the default behavior.
This is a helpful link for playing around until you find the right regexp: https://regex101.com/#javascript
var str = "var a = 2;";
// clean the duplicate whitespaces
var no_duplicate_whitespace = str.replace(new RegExp("\\s+", "g"), " ");
// and split by space
var tokens = no_duplicate_whitespace.split(" ");
Or as #kuujinbo pointed out:
str.split(/\s+/);

Regex replace linebreak and comma

i've a question about regex, i've a text and it looks like below :
car,model,serie
,Mercedes,324,1,
,BMW,23423,1,
,OPEL,54322,1,
it should look like:
car,model,serie
Mercedes,324,1,
BMW,23423,1,
OPEL,54322,1,
so without commas at the beginning of the text.
What i tried :
var str2 = str.replace(/\n|\r/g, "");
but somehow, i couldn't add comma in regex.
can anyone help me?
Thanks in advance.
There have been a lot of responses to this question and for a newbie to regex it is probably a bit overwelming,
Overall the best response has been:
var str2 = str.replace(/^,/gm, '');
This works by using ^, to check if the first character is a comma and if it is, remove it. It also uses the g and m flags to do this for the first character of every line.
If you are curious about the other versions then read on:
1:
var str2 = str.replace(/^,+/gm, '');
This is a slight variant in that it will remove multiple consecutive commas at the beginning of each line, but based off of your dataset this is not required.
2:
var str2 = str.replace(/\n,/g, '\n');
This version works exactly the same as the first, however it finds each newline follow by a comma with \n, and replaces it with another newline.
3:
var str2 = str.replace(/(\n|\r),/g, '$1')
This version is the same as the previous however it doesn't make the assumption that the newline is a \n, it instead captures any newlines or carriage returns, it works the same as the m flag and ^,.
4:
var str2 = str.replace(/\n+|\r+|,+/g,"\n")
And finally there is this, this is a combination of all the previous regex's, it makes the assumption that you may have a lot mixed newlines and commas without any text, and that you would want to remove all of those characters, it is unnecessary for your examples.
Use this syntax:
str.replace(/^,/gm, '');
You can just use multiline flag and replace leading commas:
str = str.replace(/^,+/gm);
RegEx Demo
Try:
var str2 = str.replace(/(\n|\r),/g, '$1')
Your comma was actually placed outside the regex pattern, so you weren't far off :)

JS: Adding a space between Japanese character phrase and number using regex

I need to add a space between all instances of the Japanese 丁目(chome) that are directly followed a number of unspecified digit length.
ex: 北23条東12丁目5-30-405
I have tried this (where s = "北23条東12丁目5-30-405")
s.replace(/(?:丁目)+\d+/g, "$1 ")
Since I want to add a space after a non-captured group, I thought a $1 was in order, but I am not sure how to write it (IF this replace method were to work it would probably literally output "$1 ").
Needless to say, my attempted replace() method does not work
(desired output: "北23条東12丁目 5-30-405")(<-- space after 丁目)
I'm not suprised this doesn't work... can I get pointed in the right direction?
s.replace(/(丁目)(?=\d)/g, "$1 ")
This should do it for you.Your earlier regex was not working cos if ?: which makes it non capturing and $1 had nothing in it.See demo.
https://regex101.com/r/nS2lT4/10
var re = /(丁目)(?=\d)/g;
var str = '北23条東12丁目5-30-405';
var subst = '$1 ';
var result = str.replace(re, subst);

Is there a JavaScript regular expression to remove all whitespace except newline?

How do I remove white spaces in a string but not new line character in JavaScript. I found a solution for C# , by using \t , but it's not supported in JavaScript.
To make it more clear, here's an example:
var s = "this\n is a\n te st"
using regexp method I expect it to return
"this\nisa\ntest"
[^\S\r\n]+
Not a non-whitespace char, not \r and not \n; one or more instances.
This will work, even on \t.
var newstr = s.replace(/ +?/g, '');
Although in Javascript / /g does match \t, I find it can hide the original intent as it reads as a match for the space character. The alternative would be to use a character collection explicitly listing the whitespace characters, excluding \n. i.e. /[ \t\r]+/g.
var newString = s.replace(/[ \t\r]+/g,"");
If you want to match every whitespace character that \s matches except for newlines, you could use this:
/[\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+/g
Note that this will remove carriage returns (\r), so if the input contains \r\n pairs, they will be converted to just \n. If you want to preserve carriage returns, just remove the \r from the regular expression.
Try this
var trimmedString = orgString.replace(/^\s+|\s+$/g, '') ;
This does the trick:
str.replace(/ /g, "")
and the space does NOT match tabs or linebreaks (CHROME45), no plus or questionmark is needed when replacing globally.
In Perl you have the "horizontal whitespace" shorthand \h to destinguish between linebreaks and spaces but unfortunately not in JavaScript.
The \t shorthand on the other hand IS supported in JavaScript, but it describes the tabulator only.
const str = "abc def ghi";
str.replace(/\s/g, "")
-> "abcdefghi"
try this '/^\\s*/'
code.replace(/^\s[^\S]*/gm, '')
works for me on text like:
#set($todayString = $util.time.nowEpochMilliSeconds())
#set($pk = $util.autoId())
$util.qr($ctx.stash.put("postId", $pk))
and removes the space/tabs before the first 3 lines with removing the spaces in the line.
*optimisation by #Toto:
code.replace(/^\s+/gm, '')

Categories

Resources