0

I have this an example:

const str = "Icecream, Milk, vanilla syrup, ice cubes20.0 SR180 calories"

I need a way to get the 20.0 from the string, it is always positioned before SR

I tried to convert the string to array like so:

const strArr = str.split(' ');

and try to get the index of the object contain SR

const index = strArr.map((object) => object).indexOf('SR');

But it showed me a result of -1

I was thinking to get the element by index - 1 to have the result of 20.0

Any other ideas, how to make it properly?

Thanks

2
  • Regex? str.match(/([\d\.]+) SR/)[1] Commented Jul 28, 2022 at 18:09
  • /(\d+(\.\d+)?) SR/ Commented Jul 28, 2022 at 18:09

4 Answers 4

2

If you're gonna split do it on SR. Then extract the digits from the end of the first string.

const str = "Icecream, Milk, vanilla syrup, ice cubes20.0 SR180 calories"
var arr = str.split("SR");
var rev = arr[0].trim().split("");
var num = []
while (rev.length) {
  var char = rev.pop();
  if (!(char >= '0' && char <= '9' || char == ".")) {
    break;
  }
  num.push(char);
}
var result = num.reverse().join("");
console.log(result)

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

Comments

1

I think this might help:

const finalNumber = str.split(' SR')[0].trim().split(' ').pop().replace(/[^\d.]/g, '');
console.log(finalNumber);

EDITED:

console.log("final", str.split('SR')[0].replace(/[^\d.]/g, '').split('.').slice(-2).join('.'))

3 Comments

Will fail if there are dots before the number "hello...12.0 SR"
@ITgoldman Please go through updatd answer.
I'm afraid its still not perfect. There could be other numbers before the actual required number.
1

We can use match with following regexp:

const str = "Black coffee preparing with v60 filter18.00 SR38 Calories"
const res = str.match(/\d+(.\d+)?(?= SR)/g)
console.log(res[0])

Regexp explain:

  • \d+ - will match any string of digits,
  • (.\d+)? - will match floating point (optionally thanks to ?),
  • (?= SR) - will match a group after main expression without including it in results.

2 Comments

Thank you for help, your code is work for most cases but it fails using this string: "Black coffee preparing with v60 filter18.0 SR38 Calories"
@zippax Fixed. I didn't have all your test cases.
0

This should do the trick:

const getFilter = str => str.match(/ *\d+\.\d+(?= *SR)/).map(val => val.trim());

console.log(getFilter("Black coffee preparing with v60 filter18.00 SR38 Calories")); // ["18.00"]
console.log(getFilter("Black coffee preparing with v60 filter18.0 SR38 Calories")); // ["18.0"]
console.log(getFilter("Black coffee preparing with v60 filter 18.0SR38 Calories")); // ["18.0"]
console.log(getFilter("Black coffee preparing with v60 filter18.0 SR38 Calories")); // ["18.0"]
console.log(getFilter("Black coffee preparing with v60 filter18.0SR38 Calories")); // ["18.0"]
console.log(getFilter("Black coffee preparing with v60 filter200.001 SR38 Calories")); // ["200.001"]

I added security on any space, which will then need .trim()

In case the value can only be a decimal with two digits before the decimal point, you will have to use this regex instead:

/ *\d{2}\.\d+(?= *SR)/

The best would be for you to send us all possible cases, for a definitive regex...

Good luck !

Comments

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.