Regex replace space with

890 views Asked by At

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.

4

There are 4 answers

0
Eshu On

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>

0
DougR On

You could use 2 capturing parentheses.

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

\1.\2

0
Wiktor Stribiżew On

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.

0
The fourth bird On

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