4

I already am doing a replace for the commas in an textbox. How would I replace if there is an "$" and a comma also in the same line?

function doValidate()
{
var valid = true;

document.likeItemSearchForm.sup.value = document.likeItemSearchForm.sup.value.replace(/\,/g,''); 

return valid;   
}
2
  • 2
    Why do you escape the comma? It has no special meaning in a regex. Commented Sep 26, 2011 at 13:37
  • FYI, you should tag the answer as accepted if you find it adequate. Commented Oct 12, 2011 at 19:33

1 Answer 1

10

Do you want to replace commas and dollar signs? Here's how:

"$foo, bar.".replace(/\$|,/g, "")

The regexp matches dollar signs or commas. The g flag tells it to match the entire string instead of stopping after the first match.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, I really appreciate the prompt reply!!
document.likeItemSearchForm.sup.value = document.likeItemSearchForm.sup.value.replace(/\$|,/g,'');
Works fine for me, in both Firebug and the Node.JS console.
I think /[$,]/g would be better
how to replace following character from the string using query string "[","]"

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.