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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RegExp Help in string.replace() 1

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
I have a sql command that I am using to input data into a db... problem is when I have a ' it screws up the sql command so what I need to do is do a string.replace() on the value I am inputting to replace any ' with '' (2 single quotes) any help?

string.replace(regexp,"''"); ?????
 
ok I tried this

Con1 = "hello? how are's you's guy's"
Con1 = Con1.replace("'","''");

and I get

hello? how are''s you's guy's

how can I get it to replace all of the ' not just the first
 
Con1 = "hello? how are's you's guy's"
Con1 = Con1.replace(/'/g,"''");

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
The way jemminger has shown is the best way for you to do this. Just for general interest though, you could do something like this:
Code:
Con1 = Con1.split( "'" ).join( "''" );
.

That letter 'g', the global switch, tells the search to find every instance of your character sequence in the target string, rather than just to find the first and then stop looking.
 
As a side note - how are you connecting to your DB?
If this code is in the server-side page that connects, everything should be fine :)

If it's just in the client-side javascript remember folks can turn it off, so make sure you escape those 's on the server side instead.

:)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top