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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Removing Punctuation Question

    Why is it brain surgery removing periods, question marks and exclamation points from a string in Javascript?


    So far I've gotten to this point:

    temp = temp.replace (/,/g, "");


    Any help would be greatly appreciated, thanks!

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Posts
    191
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ...

    I figured it out, thanks for reviewing:


    temp = temp.replace (/[.,?!\s,]/g, " ");

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,735
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by theflyingminstr View Post
    I figured it out, thanks for reviewing:

    temp = temp.replace (/[.,?!\s,]/g, " ");
    No, you must escape the Meta characters:-

    temp = temp.replace (/[\.\,\?!\s]/g, ""); // replace by nothing, i.e delete

    . in a regex means any character. If you want a literal period (dot) you must escape it so \.

    You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Quote Originally Posted by Philip M View Post
    Quote Originally Posted by theflyingminstr View Post
    I figured it out, thanks for reviewing:

    temp = temp.replace (/[.,?!\s,]/g, " ");
    No, you must escape the Meta characters:-

    temp = temp.replace (/[\.\,\?!\s]/g, ""); // replace by nothing, i.e delete


    . in a regex means any character. If you want a literal period (dot) you must escape it so \.
    Actually, his code is all in it's order (except for , appearing twice in it...). The set of characters that are literal (i.e. that do not have special meaning) is much larger in a character set than it is in a pattern. None of .,?! have any special meaning in a character set.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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