Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replacing spaces with  

Let's assume the following element (look for the trailing and leading spaces):

<p>
    <span class="item">Lorem Ipsum is simply dummy text </span><span class="item">of the printing and typesetting</span><span class="item"> industry.</span>
</p>

I want to replace all spaces with &nbsp;, due to display: inline-block weird behavior shown here: http://jsfiddle.net/SQuUZ/ (I don't know about all browsers, but latest Chrome and Firefox both act the same).

Now, since javascript is an option here, so is jQuery, I could:

$('p').text($('p').text().replace(/ /g, '&nbsp;'));

But it escapes the &nbsp; and turns out into a&nbsp;mess&nbsp;of&nbsp;entities.

Obviously, for such purposes we could use $('p').html():

$('p').html($('p').html().replace(/ /g, '&nbsp;'));

But this one is even worse, because, it also adds &nbsp; within the tags themselves:

<p>
    <span&nbsp;class="item">Lorem&nbsp;Ipsum&nbsp;is&nbsp;simply&nbsp;dummy&nbsp;text&nbsp;</span><span&nbsp;class="item">of&nbsp;the&nbsp;printing&nbsp;and&nbsp;typesetting</span><span&nbsp;class="item">&nbsp;industry.</span>
</p>

<!-- TL;DR -->
<span&nbsp;class="item"></span> <!-- is actually invalid... -->

And it breaks everything...

Notes:

  • There won't only be <span> elements with class item inside the container (that may also not always be <p>).
  • Slow regular expressions is an option (the problem is, I cannot come up with one...).

What options do I have here?

Update:

In fact, could anyone explain why there is such a bug with that multi-line / single-line display: inline-block;? (See fiddle link above, and examine...)

Question migrated to display: inline-block; weird spacing behavior

like image 541
tomsseisums Avatar asked May 06 '12 20:05

tomsseisums


People also ask

How do I replace a space underscore?

Use the String. replaceAll method to replace all spaces with underscores in a JavaScript string, e.g. string. replaceAll(' ', '_') . The replaceAll method returns a new string with all whitespace characters replaced by underscores.

How do you replace a space in Java?

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.


4 Answers

$('p').contents().filter(function(){
    return this.nodeType == 3 // Text node
}).each(function(){
    this.data = this.data.replace(/ /g, '\u00a0');
});

DEMO

like image 107
gdoron is supporting Monica Avatar answered Oct 19 '22 10:10

gdoron is supporting Monica


Even tho jQuery is really great and does all things, CSS could also work in some cases:

white-space: pre-wrap;

Demo.

CSS3 related: text-space-collapse

like image 29
Qtax Avatar answered Oct 19 '22 09:10

Qtax


could anyone explain why there is such a bug with that multi-line / single-line display: inline-block;? (See fiddle link above, and examine...)

Consider:

​<p><span style="display:inline-block">lorem </span>​​​​​​​​​​​​​​​​​​<span>ipsum</span></p>

The space character is inside the line box container created by display:inline-block. CSS 2.1 16.6.1 describes how spaces in a line box must be processed:

As each line is laid out … [i]f a space (U+0020) at the end of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is … removed.

As the space is at the end of the line inside the inline block, it is removed.

Contrast:

<p><span style="display:inline-block">lorem</span> <span>ipsum</span></p>​

In this case, the space is not removed, because it is between two inline-level elements that make up a single line box.

like image 3
Benjamin Hawkes-Lewis Avatar answered Oct 19 '22 09:10

Benjamin Hawkes-Lewis


you can use the <pre> </pre> element to make spaces visible. Its a fast solution if you want to visually display, lets say, ascii art.

like image 2
user1534664 Avatar answered Oct 19 '22 09:10

user1534664