Regular expressions help

Hi Guys,

I just had a quick question and wanted to know if anybody could help with a regular expression question. I am currently completing the Metric-Imperial Converter from the Information Security and Quality Assurance Projects and am looking to find the regular expression that will make sure the input string is a section of digits or a fraction and then only characters at the back. I am then splitting the string and taking the number and characters to determine the quantity and unit that needs to be converted

for example-

I’m using this to separate the digits

var reg = /[a-z,A-Z]/g;
var result = input.split(reg);

result= result[0]

these inputs will pass 2344343kg, 3443lb etc but the problem is if I have a group of characters in the middle like 455ht45lb or 5uuuu43m then it will be the wrong format. I guess I also have the same problem when I try to get the unit and it is not purely characters.

So what I want to do is run it through a check to make sure it is the correct format before splitting it. Would anyone have any ideas on this???

Your regex is basically splitting every character? I don’t see how it is matching any number at all.

By your specifications, may this regex work for you?


If not please provide the link to the challenge and I’ll re-read your post and try to understand it better.

What do you want to do with like 455ht45lb or 5uuuu43m? Discard them? Split them?

Are there limitations on the number of chars in a unit of measure?

Discard the start (which is to be the quantity, but is invalid) but keep the end(meant to be the unit)

eg (discard(455ht45) and keep(lb)) or discard(5uuuu43) keep(m).

[‘gal’,‘l’,‘lb’,‘kg’,‘mi’,‘km’] These are the only units of measurement. So between 1-3 Characters.

Im using two different regular expressions.

This one to get the first input. Which will be the quantity-

var reg = /[a-z,A-Z]/g;
var result = input.split(reg);

result= result[0]

And this one to get the unit of measurement-

   var regUnit=/[0-9]/g;
    var result=input.split(regUnit)
    result =result[result.length -1].toLowerCase();

The actual lesson can be found here-

OK then maybe try this.

var result = input.match(/\d+\.{0,1}\d*(?=(gal|l|lb|kg|mi|km)$)|(gal|l|lb|kg|mi|km)$/ig)

An input of “mi” would also pass though.