SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast indepthmedia's Avatar
    Join Date
    Apr 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript replace function with special character

    I simply want to replace all the +'s in a url string with spaces but I get an error with the following code because it is treating the + as a quantifier.

    var str="blah+blah";
    str=str.replace(/+/g, " ");

    How do I make this work?

  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    >>> str = 'blah+blah';
    "blah+blah"
    >>> str = 'blah+blah+blah';
    "blah+blah+blah"
    >>> str.replace(/\+/, '')
    "blahblah+blah"
    >>> str.replace(/\+/g, '')
    "blahblahblah"
    Directly from firebug js console


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
  •