Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Script for complex find and replace

    Hi Guys,

    I am currently building a website in French and they have a lot of numbers. The problem is, when you write numbers in french, you use a space instead of a comme (1 000 $) which means that I need to code my thousands like this (1 000 $) so it doesn't go on another line.

    I know how to find and replace in JavaScript but I don't want to search for all possible numbers one by one. I need some kind of script that will search all the numbers from 1 000 to 1 000 000 and put a   where the spaces are.

    I'm pretty noob with JavaScript and I don't even know if this is possible, but it'll be a BIG time saver for sure.

    Thanks

  • #2
    New Coder
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It works but I think I'm doing something wrong since I understand half of your code.

    Here's where I use your code

    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type = "text/javascript">
    
    
    
    function replaceNBSP(){
    var text = document.getElementById("enteryourcode").value;
    
    // works with negative numbers also
    function formatNumber(x) {
    x = x.toFixed(2).split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
    x = x.replace(/\,/g,"&nbsp;")
    document.getElementById("yourcode").value = x;
    
    }
    
    // DEMONSTRATION
    for (var i = 1000; i<12010; i = i +1100) {
    formatNumber(i);
    
    }
    } 
    
    
    function clearfields(){
    document.getElementById('yourcode').value = '';
    document.getElementById('enteryourcode').value = '';
    }
      
    
    </script>
    </head>
    
    <body>
    <div id = "output"></div>
    
    <button type = "button" onclick = "clearfields()">Clear fields</button>
    <button type = "button" onclick = "replaceNBSP()">Add nbsp; </button>
    <p>Enter your code here</p>
    <textarea name="Your code" label="Enter your code here" cols="200" rows="20" id="enteryourcode"></textarea>
    <p>Your new code is here</p>
    <textarea name="Your code" cols="200" rows="20" id="yourcode" ></textarea>
    
    <button type = "button" onclick = "clearfields()">Clear fields</button>
    <button type = "button" onclick = "replaceNBSP()">Add nbsp;</button>
    </body>
    </html>
    Basically, what I'm try to do is to past my text in the top box and when I press the button it searches for all numbers and adds &nbsp; when there's a space.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The code I posted first and deleted was based on my misunderstanding of what you wanted. I wrongly thought you wanted to put spaces into numbers like 1234567 where normally commas would go.


    I think this is what you want.


    <div id = "mytext">
    Lorem ipsum dolor 1 000 sit amet, consectetur adipiscing elit. 12 123 Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum. Morbi et metus ut diam non larum molestie 56 789 ullamcorper. Suspendisse rutrum 999 000 semper semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam 1 000 000 lobortis neque porta a sagittis urna tristique.
    </div>
    <br>

    <script type = "text/javascript">

    var text = document.getElementById("mytext").innerHTML
    text = text.replace(/(\d)(\s)(\d)/g, "$1&nbsp$3");
    alert (text);
    document.write(text);

    </script>

    <br><br>
    Note that 56 789 and 1 000 000 are now on the same line

    [/CODE]


    "Success is the ability to go from one failure to another with no loss of enthusiasm." - Sir Winston Churchill, British politician (1874 - 1965)
    Last edited by Philip M; 03-05-2013 at 06:01 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    RickGervs (03-05-2013)

  • #4
    New Coder
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Jesus this is EXACTLY what I wanted, thank you very much !!!!

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by RickGervs View Post
    Jesus this is EXACTLY what I wanted, thank you very much !!!!
    Hmmm...Philip, I know you were good, but I didn't know you are a deity.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •