Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

335
Views
How to validate string as a valid rem CSS value?

I'm doing some javascript validation on a passed in string value that should be in rem format. Looking for some guidance into Regex pattern that will match it.

This is what I have currently:

^\d*\.\d*(rem)$

However, this won't match for example (5rem). I'm sure I must be missing something simple. I'm also open to non-regex approaches.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

It's hard to know what possible values you need to match based on your question, but I think the problem could be that your current regex expects a decimal point to exist between two digits, or at least after the first.

If the rem value does not contain something all the time, you can use the ? or *. For the literal decimal match, you'll probably want to use ?.

Unless you need to match a group, the parenthesis are not necessary.

I would expect \d\.?\d*rem to work for you.

Regex101 is my preferred tool for testing expressions.

about 4 years ago · Juan Pablo Isaza Report

0

You need to check floating point conditionally. Try this:

let pattern = /^-?\d*(\.\d+)?(rem)$/
pattern.test("5rem"); // true
pattern.test("25rem"); // true
pattern.test("2.5rem"); // true
pattern.test("-2.5rem"); // true
pattern.test("5em"); // false

Not matching negative values:

let pattern = /^\d*(\.\d+)?(rem)$/
pattern.test("5rem"); // true
pattern.test("25rem"); // true
pattern.test("2.5rem"); // true
pattern.test("-2.5rem"); // false
pattern.test("5em"); // false
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!