Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    string.replace() - bleh.

    If I do a string.replace("&lt;", "<"); it replaces the first instance of &lt; in the string. I want all instances of &lt; replaced, so I did string.replace("/&lt;/g", "<");. Unfortunately it now replaces none at all.

    What's wrong with string.replace("/&lt;/g", "<");?

  • #2
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    string.replace(/&lt;/g, "<");

    you're using a string instead of a regular expression. Take the quotes out as in the above example

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle
    string.replace(/&lt;/g, "<");

    you're using a string instead of a regular expression. Take the quotes out as in the above example

    Gah.. "duh" lol. Thanks. I guess my mind was stuck in PHP mode. :P

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I should have used rather unicode escapes to be able to safely handle the string within javascript:

    string.replace(/&lt;/g,'\u003C');
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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