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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1173612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:49:57  来源:igfitidea点击:

How do I remove letters and dashes and dollar signs in a string using a regular expression?

jqueryregex

提问by Michael Stone

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.

我正在提醒以确定替换是否有效。它不是。

采纳答案by Sean

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.)

(我个人更喜欢 Mark 的更新答案,原因与他相同;您可以通过这种方式捕捉所有无法预测的内容。)

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() 调用,这样每个元素都可以单独完成。

回答by Mark Biek

This should do it

这应该做

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

The [^]is a negated character class so it's going to match all characters exceptthose 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 anythingthat's not numeric rather than having to worry about explicitly listing the non-numeric characters I don't want.

我更喜欢这种方法,因为它会捕获任何非数字字符,而不必担心明确列出我不想要的非数字字符。

If you reallyonly want to exclude letters, $, and -, then Sean'sanswer is a better way to go.

如果您真的只想排除字母、$ 和 -,那么Sean 的答案是更好的选择。

回答by tessi

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

我真的很喜欢马克的回答。但是,根据您的编程语言(和 RegEx 引擎)\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 \dequals [0-9].

如果您的 RegEx 引擎支持 UTF-8,这会更快。一个适用的示例语言是 python3(http://docs.python.org/3/library/re.html- 搜索“\d”)。在ECMA标准,然而,说对JavaScript\d等于[0-9]

回答by Mike Grace

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

Replaces all non numeric values from sumit value

替换 sum 值中的所有非数值

回答by Chaudhry Junaid

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 尝试了迈克的回答,而不是先否定 \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.

PS我测试的环境是Titanium SDK。

回答by Andrew Hare

Try something like this:

尝试这样的事情:

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