In JavaScript, you can use regular expressions to remove commas in a string through the replace() method. You only need to find all commas in the string and replace them with empty strings; syntax " String object.replace(new RegExp(",","g"),"")".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In js, you can use regular expressions to replace all commas with nothing through the replace method.
Implementation example:
Create a new html file, named test.html, to explain how js removes all commas in a string.
In the test.html file, use the input tag to create a string input box and set its id to mystr.
In the test.html file, use the button tag to create a button, bind the onclick click event to the button, and when the button is clicked, execute the delHao() function.
In the js tag, create the delHao() function. Within the function, obtain the input object through the id, obtain the input string through the value attribute, and create a replacement to remove the comma. regular expression.
In the function, use the replace method and use regular expressions to replace all commas with nothing to remove commas from the string.
Open the test.html file in the browser, enter the string, click the button to see the effect.
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to remove commas from string in javascript. For more information, please follow other related articles on the PHP Chinese website!