如何使用正则表达式删除字符串中的字母、破折号和美元符号?

时间:2021-10-24 20:17:09

I'm trying to find all letters and dashes and dollar signs and remove them from a text box.

我试图找到所有的字母、破折号和美元符号,并把它们从文本框中删除。

function numbersOnly()
{
    if ($('.sumit').val().indexOf([A-Za-z-$])) {
        $('.sumit').val().replace([A-Za-z-$], "");
    }
}

That's what I've got and I'm pretty sure it's wrong. I'm not too great with regular expressions, but I'm trying to learn them. Does anyone want to help me out and get me started with completing this function?

这就是我得到的,我很确定这是错的。我不太擅长正则表达式,但我正在努力学习它们。有人想帮我完成这个函数吗?

So.. You've got the inputs.

所以. .你必须输入。

<div class="numInputRight"><input type="text" class="sumit" name="sumAmount1"></div>
<div class="numInputRight"><input type="text" class="sumit" name="sumAmount2"></div>
<div class="numInputRight"><input type="text" class="sumit" name="sumAmount3"></div>

Then you've got the function:

然后你得到了函数:

numbersOnly = function()
{
  $('.sumit').val().replace(/[A-Za-z$-]/g, "");
  alert($('.sumit').val());
  return false;
}   

I'm alerting to determine if the replace is working. It's not.

我在提醒大家,看看替换是否有效。它不是。

6 个解决方案

#1


26  

Mark's does it for all non-digits. If you want to only take out letters, dashes and $, (but leaving decimals, for example), this modification to your original should do it:

马克对所有非数字都这样做。如果你想只写字母、破折号和$(但只留下小数),对你的原作进行修改就可以了:

$('.sumit').val().replace(/[A-Za-z$-]/g, "");

(And I personally prefer Mark's updated answer for the same reason he does; you catch everything you can't predict that way.)

(我个人更喜欢马克更新后的答案,原因和他一样;你抓住了所有你无法预测的东西。

For your updated question, the reason the values aren't changing is because val() returns a new string. It will not change the actual value. To do that, try:

对于更新后的问题,值不变的原因是val()返回一个新的字符串。它不会改变实际值。要做到这一点,试一试:

$('.sumit').each(function() { 
    $(this).val($(this).val().replace(/[A-Za-z$-]/g, "")); 
});
alert($('.sumit').val());

I also made it into an each() call so that every element would be done individually.

我还将它设置为each()调用,以便每个元素都可以单独完成。

#2


149  

This should do it

这个应该怎么做

$('.sumit').val().replace(/[^\d.]/g, "");

The [^] is a negated character class so it's going to match all characters except those listed in the character class.

[^]是一个否定字符类它会匹配所有字符中列出除字符类。

In this case, our character class is \d (which is the numbers 0-9) and a period (to allow decimal numbers).

在本例中,我们的字符类是\d(0-9)和一个句点(允许十进制数)。

I prefer this approach because it will catch anything that's not numeric rather than having to worry about explicitly listing the non-numeric characters I don't want.

我更喜欢这种方法,因为它可以捕获任何非数值的字符,而不必担心显式列出我不想要的非数值字符。

If you really only want to exclude letters, $, and -, then Sean's answer is a better way to go.

如果你真的只想排除信件,$和-,那么肖恩的答案是一个更好的方法。

#3


7  

I really like Mark's answer. However, depending on your programming language (and RegEx engine) \d matches characters like ४, or ৮, which are UTF-8 digits.

我真的很喜欢马克的回答。然而,根据您的编程语言(和正则表达式引擎)\ d像४匹配字符,或৮,utf - 8位数。

So if you only want digits from 0 to 9, use [^0-9.]:

如果你只需要数字从0到9,使用[^ 0 - 9]:

$('.sumit').val().replace(/[^0-9.]/g, "");

This is even faster if your RegEx engine is UTF-8 aware. An example Language, where this applies is python3 (http://docs.python.org/3/library/re.html - search for "\d"). The ECMA Standard, however, says that for JavaScript \d equals [0-9].

如果您的RegEx引擎支持UTF-8,那么速度甚至更快。应用这种方法的示例语言是python3 (http://docs.python.org/3/library/re.html - search for "\d")。然而,ECMA标准指出,对于JavaScript \d等于[0-9]。

#4


5  

$('.sumit').val().replace(/\D/g, "");

Replaces all non numeric values from sumit value

从sumit值替换所有非数值

#5


1  

This worked for me:

这工作对我来说:

$('.sumit').val().replace(/[^\d]/g, "");

I tried Mike's answer with the capital \D instead of negating a \d first which for some reason didn't remove parenthesis and other punctuation for me. Negating \d works perfect.

我试着用大写字母D来回答麦克的答案,而不是先否定a \D,出于某种原因,我没有删除括号和其他标点符号。否定\ d工作完美。

If you want to keep decimals, maybe put a dot and other symbols within the square brackets too.

如果你想保留小数,也可以在方括号内加上一个点和其他符号。

PS The environment in which I tested was Titanium SDK.

我测试的环境是Titanium SDK。

#6


0  

Try something like this:

试试这样:

function numbersOnly()
{
    $('.sumit').val().replace(/[\w-$]/g, "");
}

#1


26  

Mark's does it for all non-digits. If you want to only take out letters, dashes and $, (but leaving decimals, for example), this modification to your original should do it:

马克对所有非数字都这样做。如果你想只写字母、破折号和$(但只留下小数),对你的原作进行修改就可以了:

$('.sumit').val().replace(/[A-Za-z$-]/g, "");

(And I personally prefer Mark's updated answer for the same reason he does; you catch everything you can't predict that way.)

(我个人更喜欢马克更新后的答案,原因和他一样;你抓住了所有你无法预测的东西。

For your updated question, the reason the values aren't changing is because val() returns a new string. It will not change the actual value. To do that, try:

对于更新后的问题,值不变的原因是val()返回一个新的字符串。它不会改变实际值。要做到这一点,试一试:

$('.sumit').each(function() { 
    $(this).val($(this).val().replace(/[A-Za-z$-]/g, "")); 
});
alert($('.sumit').val());

I also made it into an each() call so that every element would be done individually.

我还将它设置为each()调用,以便每个元素都可以单独完成。

#2


149  

This should do it

这个应该怎么做

$('.sumit').val().replace(/[^\d.]/g, "");

The [^] is a negated character class so it's going to match all characters except those listed in the character class.

[^]是一个否定字符类它会匹配所有字符中列出除字符类。

In this case, our character class is \d (which is the numbers 0-9) and a period (to allow decimal numbers).

在本例中,我们的字符类是\d(0-9)和一个句点(允许十进制数)。

I prefer this approach because it will catch anything that's not numeric rather than having to worry about explicitly listing the non-numeric characters I don't want.

我更喜欢这种方法,因为它可以捕获任何非数值的字符,而不必担心显式列出我不想要的非数值字符。

If you really only want to exclude letters, $, and -, then Sean's answer is a better way to go.

如果你真的只想排除信件,$和-,那么肖恩的答案是一个更好的方法。

#3


7  

I really like Mark's answer. However, depending on your programming language (and RegEx engine) \d matches characters like ४, or ৮, which are UTF-8 digits.

我真的很喜欢马克的回答。然而,根据您的编程语言(和正则表达式引擎)\ d像४匹配字符,或৮,utf - 8位数。

So if you only want digits from 0 to 9, use [^0-9.]:

如果你只需要数字从0到9,使用[^ 0 - 9]:

$('.sumit').val().replace(/[^0-9.]/g, "");

This is even faster if your RegEx engine is UTF-8 aware. An example Language, where this applies is python3 (http://docs.python.org/3/library/re.html - search for "\d"). The ECMA Standard, however, says that for JavaScript \d equals [0-9].

如果您的RegEx引擎支持UTF-8,那么速度甚至更快。应用这种方法的示例语言是python3 (http://docs.python.org/3/library/re.html - search for "\d")。然而,ECMA标准指出,对于JavaScript \d等于[0-9]。

#4


5  

$('.sumit').val().replace(/\D/g, "");

Replaces all non numeric values from sumit value

从sumit值替换所有非数值

#5


1  

This worked for me:

这工作对我来说:

$('.sumit').val().replace(/[^\d]/g, "");

I tried Mike's answer with the capital \D instead of negating a \d first which for some reason didn't remove parenthesis and other punctuation for me. Negating \d works perfect.

我试着用大写字母D来回答麦克的答案,而不是先否定a \D,出于某种原因,我没有删除括号和其他标点符号。否定\ d工作完美。

If you want to keep decimals, maybe put a dot and other symbols within the square brackets too.

如果你想保留小数,也可以在方括号内加上一个点和其他符号。

PS The environment in which I tested was Titanium SDK.

我测试的环境是Titanium SDK。

#6


0  

Try something like this:

试试这样:

function numbersOnly()
{
    $('.sumit').val().replace(/[\w-$]/g, "");
}