Header Ad

Saturday, August 30, 2008

JAVASCRIPT REGULAR EXPRESSION FOR REMOVING SPECIAL SYMBOLS

The following example will give the you the notes to replace the special characters in a string.

Place the following coed with in the script tags.

var temp1;

var temp = new String('Thi\'s is a te!!!!s$t st>ri9ng... S"o??? What#...');

document.write(temp + '
');

temp = temp.replace(/[^a-zA-Z 0-9]+/g,'-');

document.write(temp + '
');

temp1=temp.replace(/\s/g,'-');

document.write(temp1 + '
');

1 comment:

Ketupia said...

Thanks Ravi. This was a timely tidbit I really needed but wasn't able to find. Sure beats my other approach of str.getCharAt() and evaluating if it was in the right ascii range.