0

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

1
  • 2
    why are you escaping the , ? Commented Jul 2, 2013 at 11:32

1 Answer 1

4

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,'');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.