JasperZelf
JasperZelf

Reputation: 2844

get price from string

I am trying to get a price and the plus or minus from a string using 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)

should output:
direction = -
amount = 0.25

What regexes should I use?

Upvotes: 0

Views: 1551

Answers (3)

alinsoar
alinsoar

Reputation: 15813

"[^€]*€[ ]*\([.[:digit:]]*\).*" -> "\1"

This is POSIX. You can use it so:

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

Upvotes: 1

Bergi
Bergi

Reputation: 665574

In JavaScript (there are no jQuery regexes), use

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

Upvotes: 2

berty
berty

Reputation: 2206

Try something like that :

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

Upvotes: 0

Related Questions