0

I'm trying to do something like this...

f = f.replace('   ','   ','gi');

I've also tried this...

f = f.replace(/\ {3,}/g,'   ','gi');

How do I replace three spaces with the entity above?

Using \s does not work, I've gone through plenty of other Q&As on Stack and nothing has worked.

For clarification this problem stems from XMLSerializer() parsing in addition to serializing and the spaces are at the beginning of each sentence. I use this to make code human readable when editing.

Example placeholder text in the XHTML editor (just a textarea)...

 <p>1
 &#160; 2
 &#160; 3
 &#160; 4
 &#160; 5</p>

...the goal is to serialize this code and either retain the entities (serialization should not parse but all the browsers do) or to "de-parse" by replacing the after-effects.

Also the variable f is a string, not an object or fragment in example.

IMPORTANT!

After using encodeURI turns out these aren't spaces being generated by XMLSerializer(), (spaces added to make this more readable) %0A %20 %C2 %A0 %C2 %A0.


Here are a couple examples of what the text looks like before and after using encodeURI to determine what the characters were since they weren't matching spaces thus throwing people off who were trying to help...

%0A%20&#160;&#160;First%20and

&#160;&#160;First and

The first has two entities inserted, not desirable, I just need one.

&#160; Which is actually

%0A%20&#160;%20Which%20is%20actually

The second output works great using the following from @Bergi...

f = f.replace(/\u00a0/g, '&#160;')

The following are the unsuccessful attempts with the last one being the successful attempt...

//f = f.replace(/^\s+|\s+$/g,'');
//f = f.replace('   ',' &#160; ','gi');
//f = f.replace( / {3,}/g, '&#160;' );
//f = f.replace(/ {3,}/g,' \u00a0 ');
//f = f.replace(/\ {3,}/g,' &#160; ');
f = f.replace(/ \u00a0/g, ' &#160;');
5
  • 3
    Doesn't replace only take 2 parameters?
    – gen_Eric
    Jul 27, 2012 at 19:57
  • What does f look like?
    – gen_Eric
    Jul 27, 2012 at 19:58
  • You should use replace with only with the two standard parameters. Anyway, ignorecase makes no sense for whitespaces, and your regex already has the global flag.
    – Bergi
    Jul 27, 2012 at 20:14
  • 1
    @John Now you've edited the question to show us what you have. Please also edit it to show us what you want, as if "If I were going to type the raw HTML source for my result into a text editor, what would I type?"
    – Phrogz
    Jul 27, 2012 at 20:14
  • Please show us the code - which input you have, how you read it into JS, and how you set the output (textarea).
    – Bergi
    Jul 27, 2012 at 20:32

3 Answers 3

2
var f = "Hello      World";
f = f.replace( / {3,}/g, '&#160;' );
console.log(f);
// "Hello&#160;World"

It smells like your problem has nothing to do with the string replacement, but how you are using that to modify your HTML. What are you doing for that?

Edit: If you want the user to actually see &#160; in the browser, then you need:

f = f.replace( /\s+{3,}/g, '&amp;#160;' );

Demo: http://jsfiddle.net/wrMMy/

7
  • @John OK, great. So what are you doing?
    – Phrogz
    Jul 27, 2012 at 20:02
  • Tried, sorry no dice. Keep in mind that the spaces are at the beginning after a line break and not in the middle of text.
    – John
    Jul 27, 2012 at 20:09
  • @John I've just showed you that this works. Your problem lies outside of your string replacement, and what you are doing with it. This is what we need to know, to help you. What is your actual code to use f after the result?
    – Phrogz
    Jul 27, 2012 at 20:10
  • Alright, I've updated this. I've encountered this problem before when trying to write directly to the DOM and I got around it by working with a string. Thing is variable f IS a string so I'm not sure how with f being a string to start with and then receiving code and then thirdly having regex work (@Hugo's code work on jsfiddle but not in my code) means something is screwy.
    – John
    Jul 27, 2012 at 20:18
  • @John Hugo's works because you are seeing the string contents in an alert box, where HTML is not interpreted. If you wrote alert('<b>hi</b/>') it would not appear in bold, you would see the markup. Similarly, when you alert the string "&#160;" you see the entity. However, when you inject that string into the web browser as HTML, it becomes an invisible whitespace character.
    – Phrogz
    Jul 27, 2012 at 20:22
1

Instead of replacing with an entitiy, replace with the actual character:

 f = f.replace(/ {3,}/g,' \u00a0 ');

Of course that depends on the output of that string, but usually you should use a string as it should be.

BTW: To make html/xml whitespaces readable, I'd recommend the css property white-space:pre-wrap instead of inserting non-breaking characters. They especially make copying the text a horror. Also, if your desire is to show as many whitespaces as in the source, you should replace /\s{2}/ with " \u00a0".


OK, you have a very curious input: linebreak (10, %0A, "\n"), a normal space (32, %20, " ") and two non-breaking spaces (160, %C2%A0, "\u00a0"). What you could do now to get the desired output:

  • replace the first nbsp with its entity and the second with a blank: replace(/\u00a0{2}/g, "&#160; ")
  • replace only the first of two adjacent nbsp: replace(/\u00a0(.)/g, "&#160;$1")
  • replace three whitespaces with two blanks and a entity between: replace(/\s{3}/g, " &#160; ")
9
  • That would do the opposite of what I'm trying to do.
    – John
    Jul 27, 2012 at 20:06
  • 1
    What exactly are you trying to do? You want to show the user a bunch of entities?
    – Bergi
    Jul 27, 2012 at 20:11
  • Sentences are properly typed with two spaces between. For human readable editing I use a line-break and then [space][entity 160][space].
    – John
    Jul 27, 2012 at 20:20
  • And what exactly does not work? How do you set the value of your editor textarea?
    – Bergi
    Jul 27, 2012 at 20:24
  • 1
    So, you want to replace(/\u00a0/g, "&#160;")?
    – Bergi
    Jul 27, 2012 at 20:31
1
f = f.replace(/\ {3,}/g,' &#160; ');

That works. replace takes just two arguments.
Demo: http://jsfiddle.net/SjydF/

4
  • 3
    No need to escape the space in the regex.
    – Phrogz
    Jul 27, 2012 at 20:01
  • That works there but no in my code. Variable f is a string and not a fragment although coming from serialization.
    – John
    Jul 27, 2012 at 20:21
  • 1
    Using encodeURI turns out these aren't spaces, (spaces added to make this more readable) %0A %20 %C2 %A0 %C2 %A0. Keep in mind this is from serialization output.
    – John
    Jul 27, 2012 at 20:29
  • Up-voting, was on-topic before I mentioned the serialization.
    – John
    Jul 27, 2012 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.