Reputation: 13
so I want to replace each '+' in this string to a space ' '
EST++++++++++%5E0+90310++162
So the output I want is:
EST %5E0 90310 162
I've tried this:
var l = l.replace(/\+/g, " ");
Which works alittle except when they occur consecutively, it replaces all the consecutive +'s with a single space.
So I'm getting this instead of what I want:
EST %5E0 90310 162
Upvotes: 0
Views: 83
Reputation: 29012
My psychic powers tell me that you are actually getting multiple spaces just fine, but you are displaying it as HTML and there (as explained here) consecutive whitespace is collapsed to one space.
EDIT: In fact, it appears that exactly this happened to your question itself when you posted it, and caused some confusion in this thread ;)
If you want to keep the whitespace, either replace it with a non-breaking space (
in HTML - but this will modify the value of the string) or display it in a different way which preserves whitespace, for example inside a <pre>
element of by using the CSS property white-space: pre;
on the containing element.
See this example:
var value = 'EST++++++++++%5E0+90310++162'.replace(/\+/g, " ");
document.getElementById('element1').innerHTML = value;
document.getElementById('element2').innerHTML = value;
<p>
<span id="element1"></span>
</p>
<p>
<pre id="element2"></pre>
</p>
(Or, if you are assigning the content using .innerHTML
like I did in my snippet, the solution could be as simple as changing to .innerText
. But I don't know where you use this code exactly so it can be that this solution doesn't apply.)
Upvotes: 3
Reputation: 2591
Working fine for me, maybe the way you are outputting the value is trimming extra spaces?
var l = 'EST++++++++++%5E0+90310++162'
//'EST %5E0 90310 162'
l = l.replace(/\+/g, " ");
console.log(l);
Upvotes: 0