Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex expression for `font-size` css parsing

Hi could please tell me what is the proper regex expression for parsing font-size css style? I mean that for any of these:

15px
15.23em
234.43rem
12.342pt

I would like to get pairs with the value and unit i.e. (15, px), (15.23, em) etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The easiest way is to simply solve it with 2 regex patterns and parse them into a string literal. To get a chain of connected numbers you can use /[0-9]+/ where the + will not only take the first number but all numbers connected to it. to include a decimal dot you need to use \. and can combine them to /[0-9]+\.[0-9]/.

To filter for letters you can use [aA-zZ] that includes both lowerCase and upperCase letters.

const VALUE = '15.6rem';

console.log(`(${VALUE.match(/[0-9]+\.[0-9]+/)}, ${VALUE.match(/[aA-zZ]+/)})`);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading