Matt Elhotiby
Matt Elhotiby

Reputation: 44066

How to remove periods in a string using jQuery

I have the string R.E.M. and I need to make it REM

So far, I have:

$('#request_artist').val().replace(".", "");

...but I get RE.M.

Any ideas?

Upvotes: 9

Views: 6991

Answers (3)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

You could pass a regular expression to the replace method and indicate that it should replace all occurrences like this: $('#request_artist').val().replace(/\./g, '');

Upvotes: 4

joshuafreelance
joshuafreelance

Reputation: 68

The method used to replace the string is not recursive, meaning once it found a matching char or string, it stop looking. U should use a regular expression replace.

$("#request_artist").val().replace(/\./g, '');

Check out Javascript replace tutorial for more info.

Upvotes: 2

Pekka
Pekka

Reputation: 449495

The first argument to replace() is usually a regular expression.

Use the global modifier:

$('#request_artist').val().replace(/\./g, "");

replace() at MDC

Upvotes: 17

Related Questions