locked
Replace all unwanted characters with a regular expression - how?

    Question

  • 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!

    Wednesday, July 09, 2008 9:10 PM

Answers

  • Hi,

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

    should do it.

    Good luck,

    John
    • Marked as answer by BlackCatBone Thursday, October 09, 2008 8:07 PM
    Thursday, July 10, 2008 12:31 AM

All replies

  • Hi,

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

    should do it.

    Good luck,

    John
    • Marked as answer by BlackCatBone Thursday, October 09, 2008 8:07 PM
    Thursday, July 10, 2008 12:31 AM
  • You may want to try \D \W (not digit, not character), include \S if you want "not white space"
    Thursday, July 10, 2008 3:23 PM