Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

339
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda