views:

64

answers:

3
return str.replace(/[\(\)\.\-\s,]/g, "")
+5  A: 
return preg_replace('/[\(\)\.\-\s,]/', '', $str);

For what it's worth, most of those backslashes are unnecessary (in either language). Parentheses and dots do not need to be escaped inside of character classes. You could simplify that to this if you wish:

return preg_replace('/[().\-\s,]/', '', $str);
John Kugelman
You can move the `-` to the end: `/[().\s,-]/`
Felix Kling
@Felix Yes, you're right, although that toes the line between "clean" and "a bit too clever".
John Kugelman
+1  A: 
$string = preg_replace('/[\(\)\.\-\s,]/','',$string);

Simple as that.

Note: The modifier g in php does not exists

RobertPitt
The modifier `g` does not exist in PHP PCRE: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
Felix Kling
Yup, i edited it before you posted sir.
RobertPitt
+2  A: 
return preg_replace('/[().\s,-]/', '', $str);

You don't need to escape all those characters in a character class (neither in JavaScript nor PHP).

Tim Pietzcker