Can Anyone help with Regex

The function bellow replaces a Fahrenheit degree with its Celsius equivalent:

function f2c(x) {
function convert(str, p1, offset, s) {
return ((p1 - 32) * 5/9) + ‘C’;
}
let s = String(x);
// Can not understand Regex bellow
let test = /(-?\d+(?:.\d*)?)F\b/g;
return s.replace(test, convert);
}

1 Like

Oh I think I have done something like this before in my GitHub. I’ll check and get back to you if I have it

1 Like

Hi,
Use https://regexper.com, it clears it up
This is what I understand from it:
optionally look for a - (minus) > one or more digits > optionally any character > and optionally one or more digits > finally for F
-2F, 2F, -30F, 30F, -2.2F, 2.2F, -30.7F, 30.7F
the \b word boundary I suppose the end of the input?

1 Like

Hi,
This expression ​(?:.\d*) confused me when ? comes first then : means any charecter?
Why does not come after (:.)
Thank you anyway

Hi @jenanazzubayd2671382

I have not much time to help you right now and I am still learning this too but you might want to check this. Its probably what you wanted to know:
MDN: " (?:x)
Non-capturing group:
Matches “x” but does not remember the match. The matched substring cannot be recalled from the resulting array’s elements ( [1], ..., [n] ) or from the predefined RegExp object’s properties ( $1, ..., $9 )."

The link is here, I hope that helps: Regular expression syntax cheatsheet - JavaScript | MDN

Happy coding! :smile:
P.S. And don’t be too hard with yourself and move on, back to that later, I am sure you’ll figure it out. :fist:

Hi, thanks a lot I will check the link , also this example form MDN
I know they are hard to remember but try to practice :slight_smile:

2 Likes