Utku Dalmaz
Utku Dalmaz

Reputation: 10172

javascript replace() function strange behaviour with regexp

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

http://jsfiddle.net/Yb2yV/

how can i replace () and spaces too?

Upvotes: 0

Views: 227

Answers (7)

Bergi
Bergi

Reputation: 664538

The brackets indicate a capturing group in the regexp. You'd need to escape them (/\(\)-/) to match the sequence "()-". Yet I guess you want to use a character class, i.e. a expression that matches "(", ")" or "-"; for whitespaces include the \s shorthand:

value.replace(/[()-\s]/g, "");

You might want to read some documentation or tutorial.

Upvotes: 0

apsillers
apsillers

Reputation: 115950

You want to match any character in the set, so you should use square brackets to make a character set:

document.write($("#a").val().replace(/[()\- ]/g,''));

Normally, parentheses have a special meaning in regular expressions, so they were being ignored in your regex, leaving just the dash. Normally, to get literal parentheses, you need to escape them with \ (but in a square bracket block, as above, you don't).

The dash above is escaped because it has normally indicates range in a character set, e.g., [a-z].

Upvotes: 0

Jashwant
Jashwant

Reputation: 29005

May be very bad but a very basic approach would be,

document.write($("#a").val().replace(/(\()|(\))|-| |/g,''));​​

| means OR, \ is used for escaping reserved symbols

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074335

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":

document.write($("#a").val().replace(/[()\-]|\s/g,''));​

(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:

document.write($("#a").val().replace(/\D/g,''));​

\D means "not a digit" (note that it's a capital, \d in lower case is the opposite [any digit]).

More info on the MDN page on regular expressions.

Upvotes: 5

Imp
Imp

Reputation: 8609

Use this

.replace(/\(|\)|-| /g,'')

You have to escape the parenthesis (i.e. \( instead of (). In your regexp, you want to list the four items: \(, \), '-' and (space) and as you want to replace any of them, not just a string of them four together, you have to use OR | between them.

Upvotes: 0

frostmatthew
frostmatthew

Reputation: 3298

document.write($("#a").val().replace(/[\s()-]/g,''));​

That will remove all whitespace (\s), parens, and dashes

Upvotes: 0

6502
6502

Reputation: 114481

You need to use a character class

/[-() ]/

Using "-" as the first character solves the ambiguity because a dash is normally used for ranges (e.g. [a-zA-Z0-9]).

Upvotes: 3

Related Questions