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 8 of 8

Thread: String Question

  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    str = str.replace(/'/g, '`');
    - NS 666
    .net DEVILoper

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks but I'm pretty weak in JS, a full function would be nice

  • #4
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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

  • #5
    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
    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>
    Last edited by Kor; 02-23-2006 at 05:01 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts

    your suggestions would be henceforth put into action...
    - NS 666
    .net DEVILoper

  • #7
    New Coder
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you guys helped me alot

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.


  •  

    Posting Permissions

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