Home >Web Front-end >JS Tutorial >How to remove all commas from a string in javascript

How to remove all commas from a string in javascript

青灯夜游
青灯夜游Original
2021-11-24 18:43:1710766browse

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, "")".

How to remove all commas from a string in javascript

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.

How to remove all commas from a string in javascript

2. In the test.html file, use the input tag to create a string input box and set its id to mystr.

How to remove all commas from a string in javascript

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 .

How to remove all commas from a string in javascript

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.

How to remove all commas from a string in javascript

#5. Within the function, use the replace method and use regular expressions to replace all commas with nothing to remove commas from the string.

How to remove all commas from a string in javascript

can also be written as:

How to remove all commas from a string in javascript

Open the test.html file in the browser, enter the string, and click the button , to see the effect achieved.

How to remove all commas from a string in javascript

How to remove all commas from a string in javascript

[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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn