Home » Javascript JQuery Examples » Remove Comma in Javascript and jQuery

Remove Comma in Javascript and jQuery

The below code shows how to remove all commas from the input string.

Using Javascript


javascript code snippet/ example
// Using String replace() method
var originalString = "1,000,000";
var stringWithoutCommas = originalString.replace(/,/g, '');

console.log(stringWithoutCommas); // Output: "1000000"

In this example, we use the replace() method with a regular expression /,/g to match all commas in the string and replace them with an empty string.


Using jQuery


jquery code snippet/ example
<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <p id="text">1,000,000</p>
    <script>
        $(document).ready(function() {
            var originalText = $('#text').text();
            var textWithoutCommas = originalText.replace(/,/g, '');
            $('#text').text(textWithoutCommas);
        });
    </script>
</body>
</html>

In this jQuery example, we select the element with the ID “text,” retrieve its content using text(), remove commas using replace(), and then set the modified text back to the element using text() again.


popular readings: