Pravin Tukadiya Pravin Tukadiya - 7 months ago 18
Javascript Question

How to Add $ in digit using Jquery

I want to add $ sign when any number found in my string.

ie.

"This 123 and if we add 2 in 123 than it becomes 125." //-this is string not a question.

out is like.

This $123 and if we add $2 in $123 than it becomes $125.

Answer

You can do it like by using regex and replace's with its callBack signature,

var str = "This 123 and if we add 2 in 123 than it becomes 125";
str = str.replace(/\d+/g, function(v){
 return "$" + v;
});

console.log(str); //This $123 and if we add $2 in $123 than it becomes $125

DEMO

Comments