删除点,逗号和"$"以外的非数字字符? [英] Remove non-numeric characters except points, commas and '$'?

查看:93
本文介绍了删除点,逗号和"$"以外的非数字字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个函数来删除除数字+字符外的所有字符:"$",."和','.

I need a function to remove all characters except numbers + characters: '$', '.' and ','.

我该怎么做?

推荐答案

> 'worth $12,345.00 dollars'.replace(/[^0-9$.,]/g, '')
"$12,345.00"

这是您要求的答案.我不建议将其用于提取货币,因为它可能会遇到如下问题:

This is the answer you asked for. I would not recommend it for extracting currencies, since it can suffer from problems like this:

> 'A set of 12 worth between $123 and $456. A good buy.'.replace(/[^0-9$.,]/g, '')
"12$123$456.."

如果您只想提取类似货币形式的表达式,则可以执行以下操作:

If you want to just extract expressions of a currency-like form, you could do:

> 'set of 12 worth between $123.00 and $45,678'.match(/\$[0-9,]+(?:\.\d\d)?/g)
["$123.00", "$45,678"]

如果您需要更复杂的匹配(例如,您只想提取美元价值而忽略美分价值),则可以执行

If you need more complicated matching (e.g. you'd just like to extract the dollar value and ignore the cent value) you could do something like How do you access the matched groups in a JavaScript regular expression? for example:

> var regex = /\$([0-9,]+)(?:\.(\d\d))?/g;
> while (true) {
>     var match = regex.exec('set of 12 worth between $123.00 and $45,678');
>     if (match === null)
>         break;
>     console.log(match);
> }
["$123.00", "123", "00"]
["$45,678", "45,678", undefined]

(因此请注意,javascript regexp对象不是不可变的/最终的对象,但是具有状态,并且可以如上面所演示的那样用于迭代.因此,您不能重用" regexp对象.即使传递myRegex2 = RegExp(myRegex)也会混合状态;对于构造函数来说,这是一个非常糟糕的语言决定.请参见有关如何在javascript中正确克隆正则表达式的附录.)如果愿意,您可以将以上内容重写为非常特殊的for循环:

(Thus be careful, javascript regexp objects are not immutable/final objects, but have state and can be used for iteration as demonstrated above. You thus cannot "reuse" a regexp object. Even passing myRegex2 = RegExp(myRegex) will mix state; a very poor language decision for the constructor. See the addendum on how to properly clone regexes in javascript.) You can rewrite the above as a very exotic for-loop if you'd like:

var myString = 'set of 12 worth between $123.00 and $45,678';
var regex = '\$([0-9,]+)(?:\.(\d\d))?';

for(var match, r=RegExp(regex,'g'); match=regex.exec(myString) && match!==null; )
    console.log(match);


附录-为什么您不能重用javascript RegExp对象

不良的语言设计,展示了状态的重用方式:

Bad language design, demonstrating how state is reused:

var r=/(x.)/g
var r2 = RegExp(r)

r.exec('xa xb xc')
["xa", "xa"]
r2.exec('x1 x2 x3')
["x2", "x2"]

如何在javascript中正确克隆正则表达式(您必须使用字符串定义它):

How to properly clone a regex in javascript (you have to define it with a string):

var regexTemplate = '(x.)'

var r = RegExp(regexTemplate, 'g')
var r2 = RegExp(regexTemplate, 'g')

r.exec('xa xb xc')
["xa", "xa"]
r2.exec('x1 x2 x3')
["x1", "x1"]

如果您希望以编程方式保留诸如'g'之类的标志,则可以使用regexTemplate = ['(x.)', 'g']; RegExp.apply(this, regexTemplate).

If you wish to programmatically preserve flags such as 'g', you can probably use regexTemplate = ['(x.)', 'g']; RegExp.apply(this, regexTemplate).

这篇关于删除点,逗号和"$"以外的非数字字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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