.Net ramblings
# Tuesday, 02 August 2005
How to properly use the javascript string replace function

i've seen a ton of different approaches to people using the javascript string function, including some homemade versions. 

the javascript replace function uses regular expressions, so it doesn't work like the C# or VB functions.  (i hoped it would)

what is also unusual for c# programmers is that the pattern you pass into the function does not get wrapped in quotes.

example: to replace all single-quotes in a string variable (called s) with the ` character:

s = s.replace(/'/gi, '`');

if that looks like gobbledegook i'll explain. the first / character starts the pattern, and the ' character is what we want to replace. the second / character ends the pattern and allows us to include options for the regex parser. g means global and i means ignore case.

i'm really only posting this so i'll remember it myself!


Tuesday, 02 August 2005 13:43:45 (GMT Daylight Time, UTC+01:00)  #    Comments [1]  General

Saturday, 28 February 2009 20:54:50 (GMT Standard Time, UTC+00:00)
Thanks for that, trying to convert all my ' string characters to "

Part way there now. :-)
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview