1

I have a block of code that is scraping a very poorly formatted price. I want to just use regular expressions to clean it up.
They are sending

$ 3 58

I want to remove the $ and space and replace the space in between dollars and cents with a . so I get:

3.58

I can get rid of the $ and leading space but I am really stuck on the substitution. I know that

  Regex (.*) returns everything
  Regex \ (.*) returns '3 58'

But what I really want is

  3.58

I am trying to do this inside of a program called content grabber so all I have access to is the regular expressions.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
personalt
  • 810
  • 3
  • 13
  • 26

4 Answers4

0

You could use 2 capturing parentheses.

\D*(\d+)\D*(\d+)

\1.\2

DougR
  • 3,196
  • 1
  • 28
  • 29
0

It seems all you need is to capture two space-separated digit chunks and use a command that will join the groups with a dot:

(\d+) (\d+)
return $1.$2

Since the return command just returns what was matched, there is no need to match anything else int the input.

The (\d+) (\d+) pattern matches and captures into Group 1 ($1) one or more digits, then a space is matched (\s+ can be used to match 1+ whitespace chars) and then (\d+) pattern again matches and captures into Group 2 ($2) one or more digits.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

console.log(' $ 1 20'.replace(/[^0-9\ .]/g, '').trim().replace(/\ /g, '.'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Eshu
  • 185
  • 6
0

In $ 3 58 you could match what you want to remove in the replacement and capture what you want to keep.

Match a dollar sign \$ and then match one or more whitespace characters \s+ and capture in a group one or more digits (\d+)

In the replacement use group1 . group2. For example $1.$2

\$\s+(\d+)\s+(\d+)

The fourth bird
  • 154,723
  • 16
  • 55
  • 70