Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to replace everything except numbers and a decimal point

People also ask

How do I change everything except numbers?

To remove all characters except numbers in javascript, call the replace() method, passing it a regular expression that matches all non-number characters and replace them with an empty string. The replace method returns a new string with some or all of the matches replaced.

How do you write a decimal number in regex?

A regular expression for a decimal number needs to checks for one or more numeric characters (0-9) at the start of the string, followed by an optional period, and then followed by zero or more numeric characters (0-9). This should all be followed by an optional plus or minus sign.

How does regex replace work?

The REGEXREPLACE( ) function uses a regular expression to find matching patterns in data, and replaces any matching values with a new string. standardizes spacing in character data by replacing one or more spaces between text characters with a single space.


Use this:

document.getElementById(target).value = newVal.replace(/[^0-9.]/g, '');

Removing only decimal part can be done as follows:

number.replace(/(\.\d+)+/,'');

This would convert 13.6667px into 13px (leaving units px untouched).


Try this:

document.getElementById(target).value = newVal.replace(/^\d+(\.\d{0,2})?$/, "");

Check the link Regular Expression Demo

use the below reg exp

[a-z] + [^0-9\s.]+|\.(?!\d)