Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace (String)

Status
Not open for further replies.

zzfive03

Programmer
Joined
Jun 11, 2001
Messages
267
I want to get rid of single quotes out of my string, It does not appear to be working. Should I be doing something differently than the example:

var temp = "Mark's House";
Re = new RegExp (/'/, 'gi');
temp = temp.replace(Re, '');
response.write (temp);

Thank you.
 
This will work:

Code:
re = /'/;
str = "Mark's House";
newstr = str.replace(re, "");
document.write(newstr)
Regards
David Byng
spider.gif

davidbyng@hotmail.com
 
Javascript doesn't use response.write(str) but document.write(str).

the ' character needs to be escape in Regular expressions. So your regular expression should look like : /\'/

You need to declare the variable Re with var in front of it.

I prefer the shorthand version for RegExp instead of :

Re = new RegExp (/'/, 'gi');

use :

var Re = /\'/gi;

My working example in my coding convention would be :

var str = "Mark's House".replace(/\'/gi, '');
document.write(str);

I hope this works for you! :)



Gary Haran
 
Thank you both!!

Worked great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top