Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Replacing "this" with "this"...

  1. #1
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Replacing "this" with "this"...

    Hello again guys ,

    Sometimes when a user types down something you may need to replace certain characters/words with others, eg -

    <html>
    <body>
    <input type="text" id="tbox">
    </body>
    </html>

    Say tbox's value was c:\document and settings\john\desktop\, what script/method would you use to replace every instance of \ with?

    Thanks ...

  2. #2
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Ummmm....

    to replace all backslashed with forward slashes

    Code:
    string.replace(/\\/g,"/")
    / / = regular expression
    \\ is an escaped backslash
    g means gloablly (All occurances)


    Last edited by Bob90; 04-12-2007 at 06:15 PM. Reason: added extra

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Say tbox's value was c:\document and settings\john\desktop\, what script/method would you use to replace every instance of \ with?
    Hmm.. it seems pcbrainbuster hasn't specified what he/she wants to replace \ with. The above regex does work, we just need to know what to replace \ with.
    - Mike

  4. #4
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Replace it with !... And can't it be done without regex???!!!

  5. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Er... replace it with what, an exclamation point?
    And can't it be done without regex???!!!
    Believe me, doing it without regular expressions would be much more complicated.
    - Mike

  6. #6
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yup, replace it with ! (remember that is to be used as an example and its not practical...), I don't understand regex (yet) so can I see the normal way of doing it (without regex)...

    Thanks ...

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Sorry, but there is no "normal" way of doing it. The replace method's arguments are regular expression strings only.
    - Mike

  8. #8
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Neverming I searched how to do it and now can do it...

  9. #9
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Code:
    <script type="text/javascript">
    // <![CDATA[
    
    function replaceIt() {
    	str = "I want to replace &#92 with a !";
    	newStr = str.replace("&#92", "!");
    
    	document.write("<b>String:</b> " +str + "<br /><b>Result:</b> " + newStr);
    }
    
    // ]]>
    </script>
    Result is

    Code:
    String: I want to replace \ with !
    Result: I want to replace ! with !

    sorry I got my code mixed up... using the replace method is alot faster then using regular expressions for simple tasks.
    Last edited by boogyman; 04-12-2007 at 08:04 PM.

  10. #10
    Join Date
    Feb 2007
    Location
    England
    Posts
    254
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default ummm

    I think you can replace words (letters) with easy regex like /word/, but other characters like backslash require more complicted regex.

Bookmarks

Posting Permissions

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