...

View Full Version : Javascript Removing Punctuation Question



theflyingminstr
08-26-2007, 06:07 PM
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!

theflyingminstr
08-26-2007, 06:24 PM
I figured it out, thanks for reviewing:


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

Philip M
08-27-2007, 07:42 AM
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

liorean
08-27-2007, 10:34 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum