SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to replace double quotes with single quotes in Javascript?!

    I have a variable in JavaScript that has double quotes in it... I need to cleanse the variable so the double quotes are replaced with single quotes... I'm currently using this:

    str_artist = str_artist.replace('"', "'");

    But it only seems to replace the first double quote with a single quote and none of the others...

    Could somebody help me here?

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    	// backslash used to escape the double quote
    	var s = "\"hi\"";
    	// regular expression used to find double quotes with the g flag for global search
    	var re = /\"/g;
    	
    	// replace with single quote
    	s = s.replace(re, "\'");
    	alert(s);

  3. #3
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I personally think replace() should work like PHP's str_replace(). Makes more sense.


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
  •