Home > Web Front-end > Front-end Q&A > How to remove commas from string in javascript

How to remove commas from string in javascript

青灯夜游
Release: 2022-02-18 18:06:12
Original
6197 people have browsed it

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

How to remove commas from 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.

Implementation example:

Create a new html file, named test.html, to explain how js removes all commas in a string.

How to remove commas from string in javascript

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

How to remove commas from string in javascript

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 commas from string in javascript

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.

How to remove commas from string in javascript

In 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 commas from string in javascript

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

How to remove commas from string in javascript

How to remove commas from string in javascript

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template