0

I have a problem with my regex, I just need to quote only the price of the products from this string, I used this Regex but it did not help me, can anyone fix it? Thank you

A word from which I only need 69

LEUWIN 4XL CHF 69.–

my try:

var re = /[0-9]{2}/;

I take it from this field (picture below)

enter image description here

It works for me in all cases except in this one

enter image description here

so far I have used this regex in function, but now I only read the first number so I have to replace, otherwise the price and the number are always the same

function calculateSumWithInput(e) {
  var re = /\d+/;
  var value = re.exec(e.target.value);
  if (value && !isNaN(value) && Number(value) === parseInt(value, 10)) {
    sum = parseInt(value, 10);
    finalInput.value =  "CHF " + sum + ".–";
  }
}

thank you all for help

11
  • Try /\b[A-Z]{3}\d+\b/. Commented Dec 6, 2018 at 14:43
  • 1
    Then /\b[A-Z]{3}\s?\d+\b/? /\b[A-Z]{3}\s?\d{2}\b/? Commented Dec 6, 2018 at 14:47
  • 2
    From your example, I would say /.*(CHF \d+).*/ Commented Dec 6, 2018 at 14:47
  • 1
    I'm sorry, I wrote a mistake, I only need 69 to quote, the last digit Commented Dec 6, 2018 at 15:03
  • 1
    Then, try: \d+(?=\.\–) Commented Dec 6, 2018 at 15:19

3 Answers 3

1

To extract digits followed by .–. Try:

\d+(?=\.\–)
Sign up to request clarification or add additional context in comments.

Comments

0

Judging from your example, I would suggest that you use this regex:

 /.*(CHF \d+).*/

You should test it, then access the second element of the array that will be the output of match. Assuming that the input is the variable text and the output must be saved to the variable price:

if(/.*(CHF \d+).*/.test(text)) price = text.match(/.*(CHF \d+).*/)[1];

Comments

0

You have to match the currency code, which always starts with three uppercase characters ([A-Z]{3}).

Followed by at least a number describing the integer part (\d+).

Followed by a period (\., we must escape the period, as it is a special character in regex).

Ending with the decimal part (\d+) or a dash -.

This leaves us with:

/[A-Z]{3} \d+\.(\d+|-)/

In any case, when making a regex it's important to give some detail about the exact pattern you want to match.

1 Comment

I'm sorry, I wrote a mistake, I only need 69 to quote, the last digit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.