简体   繁体   English

从字符串中获取价格

[英]get price from string

I am trying to get a price and the plus or minus from a string using jquery. 我正在尝试使用jquery从字符串中获取价格和正负。
I'm pretty sure regex is the way to go, but I just can't seen to get it right. 我很确定正则表达式是行之有效的方法,但是我只是看不见正确的方法。

input: 输入:
Bla bla bla bla (- € 0.25) Bla bla bla bla(-€0.25)

should output: 应该输出:
direction = - 方向=-
amount = 0.25 数量= 0.25

What regexes should I use? 我应该使用什么正则表达式?

In JavaScript (there are no jQuery regexes), use 在JavaScript中(没有jQuery正则表达式),使用

var results = input.match(/\(([+-])\s*€\s*(\d+\.\d{2})\)/);

For your input, the result of the match is: 对于您的输入, 匹配结果为:

results[0]: "(- € 0.25)"
results[1]: "-"
results[2]: "0.25"
results.index: 16
"[^€]*€[ ]*\([.[:digit:]]*\).*" -> "\1"

This is POSIX. 这是POSIX。 You can use it so: 您可以这样使用:

sed "s|[^€]*.[ ]*\([.[:digit:]]*\).*|\1|"

试试这样的事情:

/^(.+) \((\- ){0,1}€ ([0-9\.]+)\)$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM