SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Posts
    95
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replacing "." with "," problem

    It's me again ;>...
    I hope I'm not just jamming this part of the forum with my questions but am also helping others (besides myself ).

    I obviously have another problem.
    I'm having a problem when I try to replace a "." with a "," in a "number" string (i.e. "10.259).

    It's best that you check my problem here. Check the source code.

    So how can I fix this?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2002
    Location
    Penns Woods
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up Escape...

    <html><head><title>Test2</title></head><body bgcolor="white"><script>
    x = window.prompt("Leave the default number!", "10.259"); //I will change this "number" later
    alert(x +' is a '+ typeof(x)); //x is a string

    x = x.replace(/10/, "139"); //"10" in "10.259" is replaced with "139"
    alert(x); //because of the previous replacement alert displays "139.259". Works like a charm

    x = x.replace(/\./, ","); //now I want to replace "139.259" with "139,259" (in our country "," is used instead of a floating point
    alert(x); //contrary to my expectation alert displays ",39.259" instead of "139,259"

    //HOW CAN THIS BE FIXED?
    </script></body></html>

    // PS! My brain just hit a bad sector...

  3. #3
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are using regular expressions (/ /)

    In regular expressions a number of characters are special.

    . is one of them - that's why, as shown above, it must be escpaed. ( it means "match any character" )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •