Results 1 to 3 of 3

Thread: Help with parsetext and value.replace

  1. #1
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with parsetext and value.replace

    I'm trying to remove a the characters "//" from a line of parsed text.
    The text being parsed looks like this......

    //PackerSetting=0//0.0 cm
    SpringSetting=10//200 N/mm
    Gear1Setting=0//13/41 (12.615)

    I would like to remove only the double instance of the characters "//"
    and not the single instance of "/" so the text would look like this

    PackerSetting=00.0 cm
    SpringSetting=10200 N/mm
    Gear1Setting=013/41 (12.615)

    Here's the code I am using..
    Code:
    function parsetext( value )
    {
    value == ( "" );
    return value.replace(/["//"]/g, ""); value;
    }
    The Problem is that it removes the single instance of "/" also and I end up with this..

    PackerSetting=00.0 cm
    SpringSetting=10200 Nmm
    Gear1Setting=01341 (12.615)

    How do I remove the double instance only?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your function is a bit more complicated than need be. Also, your regular expression is selecting anything that matches any single character within the brackets [] (that is the meaning of brackets in this context). So, " will match as will /. Since it has the g flag, it selects all instances. Your regex could be simply:

    Code:
    /\/\//g
    The \ character is the escape character telling the regex that the two //'s are not the end of the regex. I've simplified your function and made a small demo:

    Code:
    <div id="txt"><pre>
    //PackerSetting=0//0.0 cm
    SpringSetting=10//200 N/mm
    Gear1Setting=0//13/41 (12.615)
    </pre>
    </div>
    <script type="text/javascript">
    
    String.prototype.parsetext=function (){
    return this.replace(/\/\//g, "");
    }
    
    document.getElementById('txt').innerHTML=document.getElementById('txt').innerHTML.parsetext()
    </script>
    Or, even more simply:

    Code:
    <script type="text/javascript">
    document.getElementById('txt').innerHTML=document.getElementById('txt').innerHTML.replace(/\/\//g, "");
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Brilliant!!!

    I got it worked out now.
    Thanks a bunch

    I'm getting some excellent help in this forum!

Bookmarks

Posting Permissions

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