replace(/[^0-9]/g,''));
- Replace is a method
- What does / indicate?
- What does ^ indicate along with 0-9
- What does /g indicate?
Do we need to start a regular expression with / or can we start with anything?
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.
The
/introduces a regular expression literal (just like"and'introduce string literals). A regular expression literal is in the form/expression/flags, whereexpressionis the body of the expression, andflagsare optional flags (ifor case-insensitive,gfor global,mfor multi-line stuff).The
^as the first character within[]means any character not matching the following. So[^0-9]means “any character except0through9“.The
/gends the regular expression literal and includes the “global” flag on it. Without theg,replacewould only replace the first match, not all of them.In all, what that does is replace any character that isn’t
0through9with a blank — e.g., removes non-digits. It could be written more simply as:…because
\D(note that’s an upper-caseD) means “non-digit”.MDC has a decent page on regular expressions.