目录
How to Remove all Characters Except Numbers in JavaScript
从 JavaScript 中的字符串中删除所有非数字字符
使用该String.replace()
方法从字符串中删除所有非数字字符。
该String.replace()
方法将通过用空字符串替换它们来删除字符串中除数字之外的所有字符。
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/\D/g, ''); console.log(result); // 👉️ 123456
我们传递给
String.replace()
方法的第一个参数是一个正则表达式。
正斜杠标记/ /
正则表达式的开始和结束。
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/\D/g, '');
g
(global) 标志来表示正则表达式应该匹配字符串中所有出现的地方,而不仅仅是第一次出现的地方。该\D
字符匹配所有非数字字符。
我们传递给该方法的第二个参数String.replace()
是每个匹配项的替换。
我们使用一个空字符串作为替换,以从字符串中删除所有非数字字符。
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/\D/g, ''); console.log(result); // 👉️ 123456
replace()
方法不会更改原始字符串,它会返回一个新字符串。字符串在 JavaScript 中是不可变的。我们用空字符串替换所有非数字字符,以将它们从字符串中删除。
使用字符类代替\D
特殊字符
您还可以使用包含数字范围的字符类来获得相同的结果。
const str = 'bobby 123 !@#$%^hadz?456._com'; const result = str.replace(/[^0-9]/g, ''); console.log(result); // 👉️ 123456
方括号[]
称为字符类,匹配范围内的数字。
在字符类的开头使用插入符号时^
,表示“不是以下”。
换句话说,匹配除从0
到范围内的数字以外的任何内容9
并删除匹配项。
如果您的字符串中有浮点数,这将不起作用,因为这些点.
也会被剥离。
在结果中保留点以保留浮点数
If you want to leave dots in the result to try to preserve floating-point
numbers, use the following regular expression.
const str = 'bobby 123 !@#$%^hadz?.456_com'; const result = str.replace(/[^\d.]/g, ''); console.log(result); // 👉️ 123.456
The forward slashes / /
mark the beginning and end of the regular expression.
The part between the square brackets []
is called a character class and
matches everything except for digits and dots.
The caret ^
symbol in the regex means “NOT the following”.
We match all non-digits or dots in the example.
\d
character matches all digits in the range [0-9]. However, we prefixed the character with a caret ^
(not). So in its entirety, the regex matches all non-digit characters and dots.As previously noted, things can go wrong if multiple dots exist in the string or
a dot is at the wrong place.
# Taking negative numbers into account
If you need to take negative numbers into account, slightly tweak the regular
expression.
const str = '-bobby 123 !@#$%^hadz?.456_com'; const result = str.replace(/[^\d.-]/g, ''); console.log(result); // 👉️ -123.456
Notice that we added a minus sign -
at the end of the character class.
# Handling multiple dots in the string
If you need to handle multiple dots in the string, use the parseFloat()
function on the result.
const str = '-124.45.67 $'; const result = parseFloat(str.replace(/[^\d.-]/g, '')); console.log(result); // 👉️ -124.45
We used the same regular expression to remove all non-numeric characters from
the string.
The last step is to use the parseFloat()
function to remove the part after the
second period.
const str = '-124.45.67 $'; // 👇️ -124.45.67 console.log(str.replace(/[^\d.-]/g, '')); const result = parseFloat(str.replace(/[^\d.-]/g, '')); console.log(result); // 👉️ -124.45
The parseFloat()
function takes a string as a parameter, parses the string and
returns a floating-point number.
const str1 = '123.456abc'; console.log(parseFloat(str1)); // 👉️ 123.456 const str2 = '88.123bc'; console.log(parseFloat(str2)); // 👉️ 88.123
As with parseInt()
, the parseFloat()
method ignores the non-numeric
characters at the end of the string.
额外资源
您可以通过查看以下教程来了解有关相关主题的更多信息: