Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2006, 01:33 PM   PM User | #1
Bakusozoku
New Coder

 
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Bakusozoku is an unknown quantity at this point
String Question

How can I for example can replace certain charecter from a string?
Fo example, I want to replace from a string(gets the value from an input type or text area) the charecter ' (apostrophe) to `
How can I do it?
Bakusozoku is offline   Reply With Quote
Old 02-23-2006, 01:41 PM   PM User | #2
Nischumacher
Regular Coder

 
Nischumacher's Avatar
 
Join Date: Oct 2005
Location: Bombay, India
Posts: 196
Thanks: 0
Thanked 2 Times in 2 Posts
Nischumacher has a little shameless behaviour in the past
str = str.replace(/'/g, '`');
__________________
- NS 666
.net DEVILoper
Nischumacher is offline   Reply With Quote
Old 02-23-2006, 02:12 PM   PM User | #3
Bakusozoku
New Coder

 
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Bakusozoku is an unknown quantity at this point
Thanks but I'm pretty weak in JS, a full function would be nice
Bakusozoku is offline   Reply With Quote
Old 02-23-2006, 02:49 PM   PM User | #4
Nischumacher
Regular Coder

 
Nischumacher's Avatar
 
Join Date: Oct 2005
Location: Bombay, India
Posts: 196
Thanks: 0
Thanked 2 Times in 2 Posts
Nischumacher has a little shameless behaviour in the past
with a function...
Code:
<html>
<head>
<script>
function RePLaCeR(TeXTaRea)
{ TeXTaRea.value = TeXTaRea.value.replace(/'/g, '`');
}
</script>
</head>
<body>
<textarea name="TeXTio" rows="20" cols="70"></textarea>
<br><br>
<input type="button" name=btnReplace value="Replace" onclick="RePLaCeR(TeXTio)">
</body>
</html>
without a function...
Code:
<html>
<body>
<textarea name="TeXTio" rows="20" cols="70"></textarea>
<br><br>
<input type="button" name=btnReplace value="Replace" onclick="TeXTio.value=TeXTio.value.replace(/'/g, '`');">
</body>
</html>
__________________
- NS 666
.net DEVILoper
Nischumacher is offline   Reply With Quote
Old 02-23-2006, 04:25 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
eeer... Nischumacher, document's elements must have the document object as root refrence. IE incorrecty accepts that shorthand, but objects, on a page, might belong to the window (variables, functions) or might belong to the document (tags, textNodes...). A form's element must also be referenced so.

On the other hand, if passing a string value to a function, pass it as a string, not as an object. The object and the object's name/id are different things.

Your code should have looked like
<input type="button" name="btnReplace" value="Replace" onclick="document.getElementsByName('TeXTio')[0].value=document.getElementsByName('TeXTio')[0].value.replace(/'/g, '`');">

Or use id instead of name, and getElementById(id)

But, in this particular case, it is enought to use this self reference and onkeyup event.

And, the last but not the least, I guess that the safest way to deal with special characters in javascript is to use unicode escapes (according to the charset specification). So that things are much more simplier so:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
</head>
<body>
<input type="text" onkeyup="this.value=this.value.replace(/\u0027/,'\u0060')">
</body>
</html>
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

Last edited by Kor; 02-23-2006 at 05:01 PM..
Kor is offline   Reply With Quote
Old 02-23-2006, 04:45 PM   PM User | #6
Nischumacher
Regular Coder

 
Nischumacher's Avatar
 
Join Date: Oct 2005
Location: Bombay, India
Posts: 196
Thanks: 0
Thanked 2 Times in 2 Posts
Nischumacher has a little shameless behaviour in the past

your suggestions would be henceforth put into action...
__________________
- NS 666
.net DEVILoper
Nischumacher is offline   Reply With Quote
Old 02-24-2006, 06:43 AM   PM User | #7
Bakusozoku
New Coder

 
Join Date: Feb 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Bakusozoku is an unknown quantity at this point
Thank you guys helped me alot
Bakusozoku is offline   Reply With Quote
Old 02-24-2006, 11:55 AM   PM User | #8
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
I'm guessing that you are using that input to store in the database and you want to avoid syntax error in the SQL statement. In that case, you should change the single quote in the server-side. You usually escape the single quote by using 2 single quotes to avoid error in SQL statement. Here's a sample code in case you're using ASP and SQL.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:29 AM.


Advertisement
Log in to turn off these ads.