Am i doing sth wrong or there is a problem with JS replace ?
<input type="text" id="a" value="(55) 55-55-55" />
document.write($("#a").val().replace(/()-/g,''));
prints (55) 555555
how can i replace () and spaces too?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a JavaScript regular expression, the
(and)characters have special meaning. If you want to list them literally, put a backslash (\) in front of them.If your goal is to get rid of all the
(,),-, and space characters, you could do it with a character class combined with an alternation (e.g., either-or) on\s, which stands for “whitespace”:(I didn’t put backslashes in front of the
()because you don’t need to within a character class. I did put one in front of the-because within a character class,-has special meaning.)Alternately, if you want to get rid of anything that isn’t a digit, you can use
\D:\Dmeans “not a digit” (note that it’s a capital,\din lower case is the opposite [any digit]).More info on the MDN page on regular expressions.