Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4341512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:29:42+00:00 2026-05-21T11:29:42+00:00

given a string such as one of the following: ‘2’ ‘2px’ ‘2%’ ‘2em’ ‘2foobar’

  • 0

given a string such as one of the following:

'2'
'2px'
'2%'
'2em'
'2foobar'
'foobar'

I would like to determine:

1) whether it is a plain number (2 or 2.2), and if it is round it (Math.floor()) and append ‘px’ to it, so that, ‘2’ or ‘2.2’ becomes ‘2px’

2) if it’s not a plain number determine if it is a valid css value, that is, if it consist of a number (int or float) followed by either ‘px’, ’em’ or ‘%’. I know there are others but I’ll just support those three. If it is, leave it.

3) if not, see if the string begins with a number (int ot float), round it and append ‘px’

4) and if not, set it to an empty string

So that the result would be:

'2'     -> '2px'
'2.2'   -> '2px'

'2%'    -> '2%'
'2em'   -> '2em'

'2foo' -> '2px'

'foo'  -> ''

Is it possible to do this in a compact way (maybe using regex), or by using an existing css validator library for JS?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:29:43+00:00Added an answer on May 21, 2026 at 11:29 am

    Here’s how with a regex:

    var input = '2.7em', output = parseInt(input, 10) + (input.match(/px|%|em/) || 'px')
    

    edit:

    var input = '2.7foo'
    , number = parseInt(input, 10)
    , output = (isNaN(number)) ? '': number + (input.match(/px|%|em/) || 'px');
    

    edit 2: allow for float with em and %:

    var inp='3.5em'
    , n=parseFloat(inp), p=inp.match(/%|em/)
    , output = (isNaN(n)) ? '': (p)?n+p:Math.round(n)+'px';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a string file path such as /foo/fizzbuzz.bar , how would I use bash
I would like to truncate a string such that its length is not longer
Is there a way to determine the display length of a given string (in
Given a string like this: a,string, with,various,values, and some,quoted What is a good algorithm
How can I drop all tables whose names begin with a given string? I
I have a requirement to disallow backslash characters in a given string field defined
Is there a built-in function/method that can check if a given string is a
More specifically, I'm trying to check if given string (a sentence) is in Turkish.
Given a string with a module name, how do you import everything in the
Given the string ThisStringHasNoSpacesButItDoesHaveCapitals what is the best way to add spaces before the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.