Javascript method to remove all commas in a string: 1. Use regular expressions to match all commas in the string; 2. Use the replace() method to replace all matching commas with empty characters, syntax "str.replace(/,/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.
Let’s take a look at the implementation method:
1. Create a new html file, named test.html, to explain how js removes all commas in strings.
2. In the test.html file, use the input tag to create a string input box and set its id to mystr.
3. 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 .
4. 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. Regular expression to remove commas.
#5. Within the function, use the replace method and use regular expressions to replace all commas with nothing to remove commas from the string.
can also be written as:
Open the test.html file in the browser, enter the string, and click the button , to see the effect achieved.
[Related recommendations: javascript learning tutorial]
The above is the detailed content of How to remove all commas from a string in javascript. For more information, please follow other related articles on the PHP Chinese website!