0

I would like to modify (or to format) the display of numbers with jQuery: each group of three digits separated by a space.

Now when I send data with Php, the formatting is retained. It's boring to end up with an IP address formatted, for instance!

How to fix it?

$(document).ready(function()
//$(function()
{
        $('h1, h2, h3, h4, h5, h6, p, li, dt, dd, caption, tr').each(function()
        {
            var $h = $(this);
            var html = $h.html();
            html = html.replace(/(\d{3-2})[\s\.]{0,1}(\d{3})/g, "$1 $2");
            html = html.replace(/(\d{1})[\s\.]{0,1}(\d{3})/g, "$1$2");
            // html
            $h.html(html);
    })

; });

Thanks for your help.

Regards,

Vincent

ScottE
  • 21,530
  • 18
  • 94
  • 131
Vincent
  • 325
  • 5
  • 15

1 Answers1

0

If I understood you question properly, you can achieve what you want by changing:

 html = html.replace(/(\d{3-2})[\s\.]{0,1}(\d{3})/g, "$1 $2");
 html = html.replace(/(\d{1})[\s\.]{0,1}(\d{3})/g, "$1$2");

into:

 html = html.replace(/(\d{1-3})[\s\.]{0,1}(\d{3})/g, "$1 $2");

From your text and the code I don't understand well why you do html.replace twice if your aim is to split the numbers in groups of 3 digits separated by a space

pconcepcion
  • 5,591
  • 5
  • 35
  • 53
  • Thanks. You're right! My regex is very stupid. So, I want to place space or replace dot by space beetween each group. I give another example with text : `html = html.replace(/([.]{3})/g, '…');` Three dots are replaced by html entity '…'. How to replace only the display and not the source code? Thanks. – Vincent Apr 12 '11 at 17:40
  • @Vincent. Well for replacing the dots and spaces and splitting numbers with more than 3 digits the expression I wrote `html = html.replace(/(\d{1-3})[\s\.]{0,1}(\d{3})/g, "$1 $2");` should work... To do it **only** on the content it's more complex and it's probably easier to just use wise the _jquery selectors_ to be sure you are targeting only text. Do you have an example of the input text? – pconcepcion Apr 12 '11 at 18:42
  • 1
    Thx. I just want to display just displaying text in accordance with the rules of typography. Otherwise, for numbers, I found `html = html.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 ")` from (http://stackoverflow.com/questions/2254185/regular-expression-for-formatting-numbers-in-javascript). Now, I try to improve it so that dots be recognized. :) – Vincent Apr 12 '11 at 21:34
  • @Vincent. Maybe what you are looking for is something like this `html = html.replace(/(\d)(?=([.|,]{0,1}\d\d\d)+(?![\d|,\d|.\d]))/g, "$1 ")` (remove , or . depending which separator are you using). – pconcepcion Apr 13 '11 at 03:47
  • Thank you very much. Sorry for delay. Our conversation helped me see more clearly. Vincent – Vincent Apr 13 '11 at 22:23