如何从字符串中删除数字? [英] How to remove numbers from a string?

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

问题描述

我要从字符串中删除数字:

I want to remove numbers from a string:

questionText = "1 ding ?"

我要替换数字 1 数字和问号?.可以是任何数字.我尝试了以下无效代码.

I want to replace the number 1 number and the question mark ?. It can be any number. I tried the following non-working code.

questionText.replace(/[0-9]/g, '');

推荐答案

非常接近,请尝试:

questionText = questionText.replace(/[0-9]/g, '');

replace 在现有字符串上不起作用,它返回一个新字符串.如果要使用它,则需要保留它!
同样,您可以使用一个新变量:

replace doesn't work on the existing string, it returns a new one. If you want to use it, you need to keep it!
Similarly, you can use a new variable:

var withNoDigits = questionText.replace(/[0-9]/g, '');

最后一个技巧是一次删除整个数字块,但是可能太远了:

One last trick to remove whole blocks of digits at once, but that one may go too far:

questionText = questionText.replace(/\d+/g, '');

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

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