Avatar of codefinger
codefinger
Flag for United States of America asked on

Can a javascript regular expression help clean up these odd formats?

Objective: Clean up/format value in javascript variable zheight_feet_and_inches before attempting to use it for calculations:
         
      Desired format  5' 4"   (This is what my calculation code expects.  It works properly when the numbers are received in this format.)

      Unexpected formats examples:  
         4' .5"
         5' 5.5"
         5' 11.5"
         5' 5.354"

(Numbers come from an external system over which I have no control.)    

I know I could write several if statements and string functions to clean these up,
but I know just  enough about regular expressions to think one might be useful here and save me
writing a lot of code.   But I have no idea which one or even how to write them in javascript.  

Can someone help me out here?
JavaScriptRegular Expressions

Avatar of undefined
Last Comment
David S.

8/22/2022 - Mon
SOLUTION
Steve Bink

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
codefinger

ASKER
I found that only one format actually causes problems with the subsequent calculation:

4' .5"

I was having a lot of trouble understanding this line....

$in = parseInt($y[1].trim().substring($y[1]-1)) || '0';

so I broke it down....

var splitit = zheight_feet_and_inches.split("'");
var inchside1 = splitit[1];
var inchside2 = inchside1.trim();
var inchside3  = inchside2.substring(inchside2 - 1);
var inchside4 = parseInt(inchside3);

.....and found I was always getting an NaN value in inchside4 when this was called

parseInt(inchside4)    --   The value of inchside3 at this point is .5".

Can you help me figure out what I am doing wrong?


Thanks!
codefinger

ASKER
sorry, typo,  should have said...

.....and found I was always getting an NaN value in inchside4 when this was called

 parseInt(inchside3)    --   The value of inchside3 at this point is .5".
SOLUTION
David S.

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Steve Bink

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
codefinger

ASKER
parseInt()  on the right side of the split was a bit of a red herring, but that was my own fault.  I should have mentioned I needed to convert the formats to total inches.

Splitting on the apostrophe was still key, though.   Then I just had to use the substring function to remove the quotes from the right side and use the Number function to add left and right sides back together for total inches.   Just a variation of what Steve said in the first place.

Steve would have received all the points, but I think Kravmir 's points were valid, and helped me stop trying to make Steve's code work as it was and rethink the problem.

Tempted to give a B instead of an A because neither expert mentioned whether or not a regular expression would have been helpful here.
But that is a little nitpicky, and together they both did help me solve the problem. so an A it is.

I am assuming once I click submit, I can allocate the points as I see fit and they won't automatically be split 50/50.  

Let me apolgize in advance if that is not the case.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Steve Bink

RE: Using regex, I think that would have been more complicated.  Because you're dropping the decimal portions, you would end up with a conditional replacement for something like 5' .4".  You would have to replace just those instances with "0", where all the others are being replaced with "whatever comes before the decimal".  At the least, you would have needed a series of two replacements - one for the "normal" fit, and a second for the edge case.
David S.

In the replace() method for strings in JS you can use a function as the replacement instead of a string which would allow this to be done using a single regular expression, however, in this case it's more efficient to just split the string in the first place.

Instead of using parseInt() it might actually be better to use parseFloat() and then Math.floor() or Math.round().
function pretty($x) {
  var $y = $x.split(/'\s*/),
      $ft = parseInt($y[0],10),
      $in = Math.floor(parseFloat($y[1]));
  return $ft + "'" + $in + '"';
}

Open in new window

P.S. Please don't give a lower grade when you can post a reply asking for further clarification on some point. Thank you for not downgrading our responses this time.