使用Javascript从字符串中删除数字 [英] Removing Numbers from a String using Javascript

查看:178
本文介绍了使用Javascript从字符串中删除数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Javascript从字符串中删除数字?

How do I remove numbers from a string using Javascript?

我对正则表达式不是很好但我认为我可以使用替换来实现上述?

I am not very good with regex at all but I think I can use with replace to achieve the above?

如果JQuery已经提供了这样的东西,实际上会很棒吗?

It would actually be great if there was something JQuery offered already to do this?

//Something Like this??

var string = 'All23';
string.replace('REGEX', '');



我理解在此的任何帮助。

I appreciate any help on this.

推荐答案

\d 匹配任何数字,因此您想用空字符串替换它们:

\d matches any number, so you want to replace them with an empty string:

string.replace(/\d+/g, '')

我在这里使用了 + 修饰符,以便它可以一次匹配所有相邻的数字,因此需要更少的替换。最后的 g 是一个标志,表示全局,这意味着它将替换它找到的所有匹配,而不仅仅是第一个匹配。

I've used the + modifier here so that it will match all adjacent numbers in one go, and hence require less replacing. The g at the end is a flag which means "global" and it means that it will replace ALL matches it finds, not just the first one.

这篇关于使用Javascript从字符串中删除数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆