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

245
Visualizações
What is regex for alphanumeric with no spaces?

I need to match this like:

1. 1234           true
2. 1234 5678      false
3. 1234x          true
4. x1234          true
5. abcd           false
6. abcd 1234      false

so I need to match just a string with numbers only or numbers and characters only and with no spaces (single word). This doesn't really work:

/([0-9])\w+/g
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Your pattern ([0-9])\w+ is unanchored, so it can have partial matches.

It also has to start with a digit, and at least 1 word character, have the string to be at least 2 characters long.


You can use anchors, and make sure to match a single digit. For a match only, you can omit the capture group:

^[^\W\d]*\d\w*$
  • ^ Start of string
  • [^\W\d]* Optionally match any word character except a digit
  • \d Match a single digit
  • \w* Match optional word characters
  • $ End of string

Regex demo

Note that \w can also match _

about 4 years ago · Juan Pablo Isaza Relatório

0

Match the entire string and require a at least one digit:

/^[a-z]*[0-9][a-z0-9]*$/i

See regex proof. Important: i flag (case insensitive).

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  [a-z]*                   any character of: 'a' to 'z', 'A' to 'Z' (0 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
  [a-z0-9]*                any character of: 'a' to 'z', 'A' to 'Z', '0' to '9'
                           (0 or more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        the end of the string
about 4 years ago · Juan Pablo Isaza Relatório

0

const strings = ['1234','12x3', '12 X'];
console.log(strings.filter(string=> string.match(/^[0-9a-z]+$/i)));

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