• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Regular Expressions > Replace all unwanted characters with a regular expression - how?
Ask a questionAsk a question
Search Forums:
  • Search Regular Expressions Forum Search Regular Expressions Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerReplace all unwanted characters with a regular expression - how?

  • Wednesday, July 09, 2008 9:10 PMBlackCatBone Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    Greetings,

    I need to replace all characters in a string that are not numeric or alpha.  I've got a test string that is "Nissay Dowa General-Happy".  I need to change the string so that it becomes "NissayDowaGeneralHappy".  The JavaScript that I have created removes only the first space, resulting in "NissayDowa General-Happy".  The regular expression I am using is shown below.  Can someone tell me how to do it the right way, removing all spaces and the dash character?

    mystring = "Nissay Dowa General-Happy";

    regexp = /[^A-Za-z0-9]/;

    newstring = mystring.replace(regexp, "");

    alert(newstring);

    Gracias!

    • ReplyReply
    • QuoteQuote
     

Answers

  • Thursday, July 10, 2008 12:31 AMrtizan[0] Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0
    Hi,

    regexp = /[^A-Za-z0-9]/g;

    should do it.

    Good luck,

    John
    • Marked As Answer byBlackCatBone Thursday, October 09, 2008 8:07 PM
    •  
    • ReplyReply
    • QuoteQuote
     

All Replies

  • Thursday, July 10, 2008 3:23 PMToronto Dan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    You may want to try \D \W (not digit, not character), include \S if you want "not white space"
    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement