SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replace method using javascript

    I will get the data from the server as NY^NJ^CA^MI or NC^SC etc and need to replace "^" with "," (coma) . I have tried following two approaches but not working.
    Can anyone guide me what went wrong with these approaches?

    Ex: var str="NY^NJ^CA^MI";

    var test = str.replace(/^/g, ",");

    or

    var test = str.replace(/^/g, ',');

    Thanks in advance

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Good question.

    The problem, is that the caret (^) is a special character in regular expressions which normally represents the start of the current line.
    You will need to escape it if you want to match a normal caret:

    Code:
    var test = str.replace(/\^/g, ",");

  3. #3
    SitePoint Member
    Join Date
    Mar 2014
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much.It worked


Tags for this Thread

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
  •