Given a string like:
"The dog has a long tail, and it is RED!"
What kind of jQuery or JavaScript magic can be used to keep spaces to only one space max?
Goal:
"The dog has a long tail, and it is RED!"
Given that you also want to cover tabs, newlines, etc, just replace \s\s+ with ' ':
string = string.replace(/\s\s+/g, ' ');
If you really want to cover only spaces (and thus not tabs, newlines, etc), do so:
string = string.replace(/ +/g, ' ');
Since you seem to be interested in performance, I profiled these with firebug. Here are the results I got:
str.replace( / +/g, ' ' ) -> 380ms
str.replace( /\s\s+/g, ' ' ) -> 390ms
str.replace( / {2,}/g, ' ' ) -> 470ms
str.replace( / +/g, ' ' ) -> 790ms
str.replace( / +(?= )/g, ' ') -> 3250ms
This is on Firefox, running 100k string replacements.
I encourage you to do your own profiling tests with firebug, if you think performance is an issue. Humans are notoriously bad at predicting where the bottlenecks in their programs lie.
(Also, note that IE 8's developer toolbar also has a profiler built in -- it might be worth checking what the performance is like in IE.)
var str = "The dog has a long tail, and it is RED!";
str = str.replace(/ {2,}/g,' ');
EDIT:
If you wish to replace all kind of whitespace characters the most efficient way would be like that:
str = str.replace(/\s{2,}/g,' ');
A more robust method: This takes care of also removing the initial and trailing spaces, if they exist. Eg:
// NOTE the possible initial and trailing spaces
var str = " The dog has a long tail, and it is RED! "
str = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// str -> "The dog has a long tail, and it is RED !"
Your example didn't have those spaces but they are a very common scenario too, and the accepted answer was only trimming those into single spaces, like: " The ... RED! ", which is not what you will typically need.
This is one solution, though it will target all space characters:
"The dog has a long tail, and it is RED!".replace(/\s\s+/g, ' ')
"The dog has a long tail, and it is RED!"
Edit: This is probably better since it targets a space followed by 1 or more spaces:
"The dog has a long tail, and it is RED!".replace(/ +/g, ' ')
"The dog has a long tail, and it is RED!"
Alternative method:
"The dog has a long tail, and it is RED!".replace(/ {2,}/g, ' ')
"The dog has a long tail, and it is RED!"
I didn't use /\s+/ by itself since that replaces spaces that span 1 character multiple times and might be less efficient since it targets more than necessary.
I didn't deeply test any of these so lmk if there are bugs.
Also, if you're going to do string replacement remember to re-assign the variable/property to its own replacement, eg:
var string = 'foo'
string = string.replace('foo', '')
Using jQuery.prototype.text:
var el = $('span:eq(0)');
el.text( el.text().replace(/\d+/, '') )
I have this method, I call it the Derp method for lack of a better name.
while (str.indexOf(" ") !== -1) {
str = str.replace(/ /g, " ");
}
Running it in JSPerf gives some surprising results where it beat some of the more sophisticated approaches EDIT Original JSPerf link http://jsperf.com/removing-multiple-spaces/3 seems to be dead at the time
Here is an alternate solution if you do not want to use replace (replace spaces in a string without using replace javascript)
var str="The dog has a long tail, and it is RED!";
var rule=/\s{1,}/g;
str = str.split(rule).join(" ");
document.write(str);
More robust:
function trim(word)
{
word = word.replace(/[^\x21-\x7E]+/g, ' '); // change non-printing chars to spaces
return word.replace(/^\s+|\s+$/g, ''); // remove leading/trailing spaces
}
I suggest
string = string.replace(/ +/g," ");
for just spaces OR
string = string.replace(/(\s)+/g,"$1");
for turning multiple returns into a single return also.
Also a possibility:
str.replace( /\s+/g, ' ' )
I know that I am late to the party, but I discovered a nice solution.
Here it is:
var myStr = myStr.replace(/[ ][ ]*/g, ' ');
Comprehensive unencrypted answer for newbies et al.
This is for all of the dummies like me who test the scripts written by some of you guys which do not work.
The following 3 examples are the steps I took to remove special characters AND extra spaces on the following 3 websites (all of which work perfectly) {1. EtaVisa.com 2. EtaStatus.com 3. Tikun.com} so I know that these work perfectly.
We have chained these together with over 50 at a time and NO problems.
// This removed special characters + 0-9 and allows for just letters (upper and LOWER case)
function NoDoublesPls1()
{
var str=document.getElementById("NoDoubles1");
var regex=/[^a-z]/gi;
str.value=str.value.replace(regex ,"");
}
// This removed special characters and allows for just letters (upper and LOWER case) and 0-9 AND spaces
function NoDoublesPls2()
{
var str=document.getElementById("NoDoubles2");
var regex=/[^a-z 0-9]/gi;
str.value=str.value.replace(regex ,"");
}
// This removed special characters and allows for just letters (upper and LOWER case) and 0-9 AND spaces
// The .replace(/\s\s+/g, " ") at the end removes excessive spaces
// when I used single quotes, it did not work.
function NoDoublesPls3()
{ var str=document.getElementById("NoDoubles3");
var regex=/[^a-z 0-9]/gi;
str.value=str.value.replace(regex ,"") .replace(/\s\s+/g, " ");
}
::NEXT::
Save #3 as a .js // I called mine NoDoubles.js
::NEXT::
Include your JS into your page
<script language="JavaScript" src="js/NoDoubles.js"></script>
Include this in your form field:: such as
<INPUT type="text" name="Name"
onKeyUp="NoDoublesPls3()" onKeyDown="NoDoublesPls3()" id="NoDoubles3"/>
So that it looks like this
<INPUT type="text" name="Name" onKeyUp="NoDoublesPls3()" onKeyDown="NoDoublesPls3()" id="NoDoubles3"/>
This will remove special characters, allow for single spaces and remove extra spaces.
var string = "The dog has a long tail, and it is RED!";
var replaced = string.replace(/ +/g, " ");
Or if you also want to replace tabs:
var replaced = string.replace(/\s+/g, " ");
// replace multiple white spaces with one white space
String replacedDisplayName = displayName.replaceAll("\\s{2,}", " ");
var myregexp = new RegExp(/ {2,}/g);
str = str.replace(myregexp,' ');
var text = `xxx df dfvdfv df
dfv`.split(/[\s,\t,\r,\n]+/).filter(x=>x).join(' ');
result:
"xxx df dfvdfv df dfv"
I know we have to use regex, but during an interview, I was asked to do WITHOUT USING REGEX.
#slightlytyler helped me in coming with the below approach.
const testStr = "I LOVE STACKOVERFLOW LOL";
const removeSpaces = str => {
const chars = str.split('');
const nextChars = chars.reduce(
(acc, c) => {
if (c === ' ') {
const lastChar = acc[acc.length - 1];
if (lastChar === ' ') {
return acc;
}
}
return [...acc, c];
},
[],
);
const nextStr = nextChars.join('');
return nextStr
};
console.log(removeSpaces(testStr));
here is my solutions that work well for me:
var text = " Tes ddas dMd WAlkman 3Dsfd "
.toLowerCase()
.replace(/\b\s+/g, " ")
.replace(/\b\w/g, s => s.toUpperCase())
.trimStart()
.trimEnd();
console.log(text);
// result: Tes Ddas Dmd Walkman 3dsfd
We can use the following regex explained with the help of sed system command. The similar regex can be used in other languages and platforms.
Add the text into some file say test
manjeet-laptop:Desktop manjeet$ cat test
"The dog has a long tail, and it is RED!"
We can use the following regex to replace all white spaces with single space
manjeet-laptop:Desktop manjeet$ sed 's/ \{1,\}/ /g' test
"The dog has a long tail, and it is RED!"
Hope this serves the purpose
Try this to replace multiple spaces with a single space.
<script type="text/javascript">
var myStr = "The dog has a long tail, and it is RED!";
alert(myStr); // Output 'The dog has a long tail, and it is RED!'
var newStr = myStr.replace(/ +/g, ' ');
alert(newStr); // Output 'The dog has a long tail, and it is RED!'
</script>
Read more # Replacing Multiple Spaces with Single Space
For more control you can use the replace callback to handle the value.
value = "tags:HUNT tags:HUNT tags:HUNT tags:HUNT"
value.replace(new RegExp(`(?:\\s+)(?:tags)`, 'g'), $1 => ` ${$1.trim()}`)
//"tags:HUNT tags:HUNT tags:HUNT tags:HUNT"
This script removes any white space (multiple spaces, tabs, returns, etc) between words and trims:
// Trims & replaces any wihtespacing to single space between words
String.prototype.clearExtraSpace = function(){
var _trimLeft = /^\s+/,
_trimRight = /\s+$/,
_multiple = /\s+/g;
return this.replace(_trimLeft, '').replace(_trimRight, '').replace(_multiple, ' ');
};
' mouse pointer touch '.replace(/^\s+|\s+$|(\s)+/g, "$1") should do the trick!
Using nodepad++ function, below regex works fine for me,
Find: {1}\K\s+
Replace:leave it empty
let nameCorrection = function (str) {
let strPerfect = str.replace(/\s+/g, " ").trim();
let strSmall = strPerfect.toLowerCase();
let arrSmall = strSmall.split(" ");
let arrCapital = [];
for (let x of arrSmall.values()) {
arrCapital.push(x[0].toUpperCase() + x.slice(1));
}
let result = arrCapital.join(" ");
console.log(result);
};
nameCorrection("Pradeep kumar dHital");
def removeblanks(text): return re.sub(r'\s\s+'," ",text)
I was working on a large textual data with a lot of duplicate spaces.
The above RE worked for me. All the duplicate blank spaces were replaced by a single space.
Related
For example
let myString = "This is my string";
let replacedString = myString.replace(/\ /g, "") //Thisismystring
Now that all the whitespaces have been removed, how do I put them back in the exact position?
Additionally, let's suppose the replaced string undergoes some change and becomes
let myChangedString = "(T)(h)(i)(s)(i)(s)(m)(y)(s)(t)(r)(i)(n)(g)";
Now I want to put the whitespaces back where they used to be i.e after (s) and before (i), after (s) and before (m), after (y) before (s)
I've spent a couple of hours on this and been stuck in the same position, any form of help would be greatly appreciated.
EDIT: Solved, thank you very much.
The trick here is to replace the spaces with another character - rather than just removing the space. That way - its a simple matter to replace the added character with a space to return the spaces to where they started. When I do this - I always use the tilde character "~" since it is easily recognisable as well as unlikely to actually be used in a string.
I have added a few variations / modifications as well as the example you have provided with every character being wrapped in parentheses - just note that you will need to escape these when replaceing the (~) for the " " space character.
let myString = "This is my string";
let replacedString = myString.replace(/\ /g, "~");
console.log(replacedString);//This~is~my~string
let modifiedString = replacedString.replace(/my/g, "your");
console.log(modifiedString);//This~is~your~string
let spacedString = modifiedString.replace(/~/g, " ");
console.log(spacedString);//This is your string
// using your example of wrapping each character in parentheses
let myChangedString = "(" + modifiedString.split('').join(")(") + ")";
console.log(myChangedString); //(T)(h)(i)(s)(~)(i)(s)(~)(y)(o)(u)(r)(~)(s)(t)(r)(i)(n)(g)
let mySpacedString = myChangedString.replace(/\(~\)/g, " ");
console.log(mySpacedString); //(T)(h)(i)(s) (i)(s) (y)(o)(u)(r) (s)(t)(r)(i)(n)(g)
Why not replace only the parts you need to be replaced?
For example search for word character and replace with the wanted parts.
console.log("This is my string".replace(/\w/g, '($&)'));
Better you just transform your original array. Loop through array and modify the char is not empty.
let myString = "This is my string";
let chars = [...myString].map(item => item !== ' ' ? '(' + item + ')': item)
console.log(chars.join(''))
Are you looking for this...
var result = "thisismystring".replace(/^(.{4})(.{2})(.{2})(.*)$/, "$1 $2 $3 $4");
alert(result);
I have a string like below
Hello there how are you?
I want to look for the substring 'there how' in the string. So I would do something like this
var str = "Hello there how are you?";
var term = "there how"
var res = str.match("\\s" + term + "\\s"); // # /s is used to ensure the match should be an independent phrase
But now the problem is, if I get a variation of the string, then the match doesn't occur. For example for strings like this
If there is a large amount of space between the words
Hello there how are you?
If certain letters are capitialized
Hello There How are you?
What I want to do is to ensure as long as the substring 'there how' is present in the string as a separate phrase (not like Hellothere how are you? or Hello there howare you? etc), I should be able to find a match.
How can I achieve the objective?
Thanks to #Wiktor Stribiżew, he proposed this solution below
var ss = ["Hello there how are you?", "Hello there how are you?", "Hello There How are you?"];
var term = "there how";
var rx = new RegExp("(?<!\\S)" + term.replace(/ /g, "\\s+") + "(?!\\S)", "i");
for (var i=0; i<ss.length; i++) {
var m = ss[i].match(rx) || "";
console.log(m[0]);
}
While this works in online nodejs compiler like repl https://repl.it/repls/AwkwardSpitefulAnimatronics, it won't work in regular javascript.
I get this error below in javascript for this line
var rx = new RegExp("(?<!\\S)" + term.replace(/ /g, "\\s+") + "(?!\\S)", "i");
SyntaxError: invalid regexp group
How can I achieve my objective?
Many browsers still don't support lookbehind hence you're getting that error. You may use this approach:
var ss = ["Hello there how are you?", "Hello there how are you?", "Hello, There How are you?"];
var term = "there how";
var rx = new RegExp("(?:^|\\s)(" + term.replace(/ +/g, "\\s+") + ")(?!\\S)", "gi");
var m;
for (var i=0; i<ss.length; i++) {
while(m = rx.exec(ss[i])) {
console.log('Start:', m.index, 'End:', rx.lastIndex, m[1]);
}
}
(?:^|\\s) is a non-capturing group that matches line start or a whitespace on left hand side of term.
Also note use of a capturing group to grab your desired substring from given input.
The (?<!\\S) portion of the regex string is what is causing the error, and it is happening on your regular version of JavaScript which does not support lookbehinds, even fixed width lookbehinds. One workaround would be to just use a word boundary there instead:
var rx = new RegExp("\\b" + term.replace(/ /g, "\\s+") + "(?!\\S)", "i");
Assuming your term starts and ends with word characters, \b should be sufficient to cover the behavior you want.
Below is an example using the term as part of the regex. Setup variables are from anubhava's answer.
// setup variables from other answers
var ss = ["Hello there how are you?", "Hello there how are you?", "Hello There How are you?"];
var term = "there how";
// if you want to use the term in the regex, replace the space(s) with \\s+ (1 or more spaces)
function replaceSpaces(s) {
return s.replace(/ /g, "\\s+")
}
// create regex
var pattern = new RegExp(`\\s${replaceSpaces(term)}\\s`)
// lowercase before comparing to ignore case
// if the regex needs to be case insensitive too, lowercase the output of replaceSpaces(term) as well
console.log(ss.map(s => pattern.test(s.toLowerCase())))
Depending on how you want your results to come back, you can approach the problem one of two ways. If you want the searched term to be returned exactly the way it shows up in the input, you can make the regex more general (option 1). However, if you want the results to come back matching the formatting of the search term, you can sanitize the input first to remove excess spaces and caps.
As Tim mentioned above, the \b word break should be sufficient to determine that the phrase is independent of other words in the input.
var ss = ["Hello there how are you?", "Hello there how are you?", "Hello There How are you?", "Hello There Howare you?"]
function buildRgx(term){
let spaceFix = term.split(' ').join('\\s+')
return new RegExp('\\b' + spaceFix + '\\b', 'i')
}
var generalizedSearchTerm = buildRgx("there how")
ss.forEach(str => {
let result = generalizedSearchTerm.exec(str)
if(result){
strmatch = result[0],
indexstart = result.index,
indexend = indexstart + strmatch.length
console.log(strmatch, indexstart, indexend)
} else {
console.log('no match found')
}
})
//OR sanitize the input first
console.log('OR')
function sanitizeStr(str){ return str.toLowerCase().replace(/\s+/g, ' ') }
var simpleSearchTerm = new RegExp('\\b' + "there how" + '\\b')
ss.forEach(str => {
let sanitizedString = sanitizeStr(str)
console.log(simpleSearchTerm.exec(sanitizedString))
})
I want to compare two input values on a page.
One input value is always entered with hyphens as spaces. ie "first-value"
The other input value is never entered with hyphens as spaces. ie "first value"
"first-test" == "first test"
this views them as different. Is there an operator that would view these as the same?
Dashes may come in more varieties thank you'd expect. Especially if people copy/paste their input from MS Word and the likes. For example, would you consider - or ‐ or ‑ or ‒ or – or — or ― all to be dashes? (they're all different unicode characters)
If the parts that you care about are only alphanumeric, you're better off stripping away everything else.
Do you regard first-test and firs ttest to be equal? If yes, then simply removing all non-alphanumeric chars will do:
str1 = str1.replace(/[^a-z0-9]/gi,'');
str2 = str2.replace(/[^a-z0-9]/gi,'');
var doMatch = (str1 == str2);
If no, then replace all non-alphanumeric parts with single spaces:
str1 = str1.replace(/[^a-z0-9]+/gi,' ');
str2 = str2.replace(/[^a-z0-9]+/gi,' ');
// trim to ignore space at begin or end
str1 = str1.replace(/^\s+|\s+$/g,'');
str2 = str2.replace(/^\s+|\s+$/g,'');
var doMatch = (str1 == str2);
This also allows for people copy/pasting values with an accidental extra space at the end. Which sometimes happens but is barely noticeable, and could cause lots of headaches if you consider that different.
var str1 = 'first-test';
var str2 = 'first test';
var doMatch = str1.replace('-', ' ') === str2.replace('-', ' '); // true
var variableWithHyphen = "variable-value-value";
var variable = "variable value value";
function areEqual(varOne, varTwo) {
var hypen = new RegExp('-', 'g');
return varTwo.replace(hypen, " ") === varOne.replace(hypen, " ");
}
alert(areEqual(variable, variableWithHyphen));
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', ' ')
const words = `He had
concluded that pigs
must be able
to fly in Hog Heaven.
`
document.body.innerHTML = "<pre>without-Trim-And-Remove:\n" + words + "</pre>";
trimAndRemoveSymbols=(text)=>{
return text.replace(/[\n]+/g, '').trim();
}
document.body.innerHTML += "<pre>Trim-And-Remove:\n" + trimAndRemoveSymbols(words) + "</pre>";
$("#topNav" + $("#breadCrumb2nd").text().replace(" ", "")).addClass("current");
This is a snippet from my code. I want to add a class to an ID after getting another ID's text property. The problem with this, is the ID holding the text I need, contains gaps between the letters.
I would like the white spaces removed. I have tried TRIM()and REPLACE() but this only partially works. The REPLACE() only removes the 1st space.
You have to tell replace() to repeat the regex:
.replace(/ /g,'')
The g character makes it a "global" match, meaning it repeats the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here.
If you want to match all whitespace, and not just the literal space character, use \s instead:
.replace(/\s/g,'')
You can also use .replaceAll if you're using a sufficiently recent version of JavaScript, but there's not really any reason to for your specific use case, since catching all whitespace requires a regex, and when using a regex with .replaceAll, it must be global, so you just end up with extra typing:
.replaceAll(/\s/g,'')
.replace(/\s+/, "")
Will replace the first whitespace only, this includes spaces, tabs and new lines.
To replace all whitespace in the string you need to use global mode
.replace(/\s/g, "")
Now you can use "replaceAll":
console.log(' a b c d e f g '.replaceAll(' ',''));
will print:
abcdefg
But not working in every possible browser:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
Regex for remove white space
\s+
var str = "Visit Microsoft!";
var res = str.replace(/\s+/g, "");
console.log(res);
or
[ ]+
var str = "Visit Microsoft!";
var res = str.replace(/[ ]+/g, "");
console.log(res);
Remove all white space at begin of string
^[ ]+
var str = " Visit Microsoft!";
var res = str.replace(/^[ ]+/g, "");
console.log(res);
remove all white space at end of string
[ ]+$
var str = "Visit Microsoft! ";
var res = str.replace(/[ ]+$/g, "");
console.log(res);
var mystring="fg gg";
console.log(mystring.replaceAll(' ',''))
** 100% working
use replace(/ +/g,'_'):
let text = "I love you"
text = text.replace( / +/g, '_') // replace with underscore ('_')
console.log(text) // I_love_you
Using String.prototype.replace with regex, as mentioned in the other answers, is certainly the best solution.
But, just for fun, you can also remove all whitespaces from a text by using String.prototype.split and String.prototype.join:
const text = ' a b c d e f g ';
const newText = text.split(/\s/).join('');
console.log(newText); // prints abcdefg
I don't understand why we need to use regex here when we can simply use replaceAll
let result = string.replaceAll(' ', '')
result will store string without spaces
let str = 'a big fat hen clock mouse '
console.log(str.split(' ').join(''))
// abigfathenclockmouse
Use string.replace(/\s/g,'')
This will solve the problem.
Happy Coding !!!
simple solution could be : just replace white space ask key value
val = val.replace(' ', '')
Use replace(/\s+/g,''),
for example:
const stripped = ' My String With A Lot Whitespace '.replace(/\s+/g, '')// 'MyStringWithALotWhitespace'
Well, we can also use that [^A-Za-z] with g flag for removing all the spaces in text. Where negated or complemente or ^. Show to the every character or range of character which is inside the brackets. And the about g is indicating that we search globally.
let str = "D S# D2m4a r k 23";
// We are only allowed the character in that range A-Za-z
str = str.replace(/[^A-Za-z]/g,""); // output:- DSDmark
console.log(str)
javascript - Remove ALL white spaces from text - Stack Overflow
Using .replace(/\s+/g,'') works fine;
Example:
this.slug = removeAccent(this.slug).replace(/\s+/g,'');
function RemoveAllSpaces(ToRemove)
{
let str = new String(ToRemove);
while(str.includes(" "))
{
str = str.replace(" ", "");
}
return str;
}