4

Hi this is working now but I am confused at to why.

I am learning regex and need to pull the numbers out of strings like

'Discount 7.5%' should get 7.5 'Discount 15%' should get 15 'Discount 10%' should get 10 'Discount 5%' should get 5 

etc.

/\d,?.\d?/ //works /\d,?.\d,?/ //doesn't works /\d?.\d?/ //doesn't works 

I thought one of the second two would work could someone explain this.

4
  • 1
    1. You should escape the dots . in all three regex by preceding it with \, 2. Use \d+ to match more than one digits 3. No need of , in any regex Commented Nov 5, 2015 at 4:36
  • Define "does not work". Commented Nov 5, 2015 at 4:37
  • Possible duplicate of regular expression for DOT Commented Nov 5, 2015 at 4:39
  • IMO, use (\d+(\.\d+)?) with first capture group Commented Nov 5, 2015 at 4:42

4 Answers 4

2

Quick and dirty with easy to understand regex.

//Let the system to optimize the number parsing for efficiency parseFloat(text.replace(/^\D+/, "")); 

Demo: http://jsfiddle.net/DerekL/4bnp8381/

Sign up to request clarification or add additional context in comments.

6 Comments

Will replace . decimal too
@Tushar Nope It won't.
@Tushar True, but it's there just to make sure it only removes the non-digits in the front.
@Derek朕會功夫 , this would match Fee 20% too while it is not needed
I like this and you can add parseFloat(t.replace(/^Discount /, "")) if you want to have only Strings that start with Discount good way to get rid of the comma it even works with 'Discount 10% extra' and i thought that would break it jsfiddle.net/4bnp8381/2
|
0

Try this. Make the second part with dot . optional with ?

(\d)*(\.\d*)? 

Comments

0

IF you check this Regex, this is what you need so it will return the float number only if the word before it is Discount by making use of the lookbehind operator:

(?<=Discount\s)\d+(\.\d+)? 

But the problem is that works with PHP(prce) I can't get it working with javascript.

EDIT: As mentioned in here regex and commas Javascript does not support lookbehind regex so here is what I did to work around it JS Fiddle

var div = document.getElementById('test'); var text = div.innerHTML; div.innerHTML = text.replace(/Discount (.*)%/ig,'$1');
<div id="test"> Discount 7.5%<br> Discount 15%<br> discount 10%<br> Discount 5.433%<br> Fee 11%<br> Discount 22.7% </div>

As you see it does match it only if it was followedd by the word Discount, the word Fee 11% does not match

2 Comments

@TimBiegeleisen, I'm sorry but it is not the same
My inputs are pretty standard but I do like that this checks for the word Discount. I tried starting using replace but in something like this i could not get it to work. var re = /\d,?.\d?/; var vipextract = re.exec(cusvipdis); var skuextract = re.exec(skutext);
-1

You can use the following regex, which will match one or more digits, optionally followed by a decimal point and any number of digits:

^Discount\s(\d+(\.\d+)?)%$ 

Regex101

7 Comments

@Tushar Thanks for catching this. I did not test thoroughly enough.
(\d+(\.\d+)?) better?
@Tushar I would avoid the + operator since this is a percentage. We don't want to allow for, say 4 digits in the beginning.
how about \d+(\.\d+)?
@Mi-Creativity It's the same, I just added brackets to get captured group
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.