Home  >  Article  >  Web Front-end  >  How to delete non-numeric characters in javascript

How to delete non-numeric characters in javascript

青灯夜游
青灯夜游Original
2021-12-08 13:42:306887browse

In js, you can use the replace() function with the regular expression "/[^0-9]/g" or "/[^\d]/g" to delete non-numeric characters, syntax "str.replace(/[^0-9]/g,'')" or "str.replace(/[^\d]/g,'')".

How to delete non-numeric characters in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, you can use the replace() function with regular expressions to delete non-numeric characters.

Thereplace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.

Example:

var str="0123kj45j67u89";
var nstr = str.replace(/[^0-9]/g,'');
// var nstr = str.replace(/[^\d]/g, '');
console.log(nstr);

How to delete non-numeric characters in javascript

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to delete non-numeric characters 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