0

I have this JavaScript code:

var test123 = $('product-price-' + productId).innerHTML; // thats 26,00 €
var finalPrice = test123.replace(/[^\d.,]/, ""); 

Testing my regex here: http://www.regular-expressions.info/javascriptexample.html correctly returns me 26,00, exactly what I want. Why is it not working in my code? In my code it replaces nothing at all.

Thanks!

1
  • @DavidThomas That doesn't looks like jQuery. I think that it's MooTools (or any other library where $ is a shortcut for document.getElementById). Commented Sep 24, 2012 at 12:24

1 Answer 1

2

Escape the period and add g to make it global;

/[^\d\.,]/g
Sign up to request clarification or add additional context in comments.

5 Comments

Escaping the period is unnecessary inside a character class.
@verdesmarald: Exactly, the problem must be somewhere else, but the g made it work. Solution: By adding the g I made it work.
@user1638055 The g modifier part is correct and will solve your problem.
Yeah, I noticed now, too. Can you explain why please?
@user1638055 g is a modifier to make the regex "global". Without g, the regex is only applied to the first match (so in your case it matches just & in 26,00 €), with g it is applied to every match in the string (so it matches &, n, b, s, p, ; and ).

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.