AJT
AJT

Reputation: 266

.replace() not working in JS

I'm having trouble getting the replace function to work. Here's my code:

var divid = $(this).parents(".list-radio").attr('id').match(/\d/g);
divid = divid.replace(/\,/g,"");

The first line brings back a set of digits separated by commas (e.g. "2,2,3") and I was hoping the second line would remove the commas, but it just fails.

Any ideas?

Thanks in advance,

Ash

Upvotes: 0

Views: 71

Answers (1)

David Hedlund
David Hedlund

Reputation: 129792

No, the .match gives you an array of digits, without commas. If you alert the array or try to turn it into a text representation by some other means, it may show up as a comma spearated list, but that's just a presentational artefact.

If you want the result as a string of digits without commas you may run divid.join(''). Another way would be to just remove anything that isn't a digit from the original string:

var divid = $(this).parents(".list-radio").attr('id').replace(/\D/g,'');

Upvotes: 4

Related Questions