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

191
Visualizações
How to add the option to allow only one space in a regex

I want a regex that will allow only one space within a set of characters. I have the below regex which doesn't accept space. The below regex only supports strings like

1. @alia 2. @ranbir 3. @shilpa

const regex = /@(\w+([a-z]|[A-Z]|[0-9]|[-]|[_]|[.]|['])*)$/g;

I want a regex that will allow strings like

1. @alia bhat 2. @ranbir kapoor 3. @shilpa shetty

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can try something like this:

@([\w\-\.\']+(?:\s[\w\-\.\']+)?)$

Explanation: group all the chars in a single set (\w = a-zA-Z0-9_ + the rest of the chars)

then a next group would optionally match a single space followed by at least one character of the same set of characters, this means that your current single words will match and also two words but not if there is two spaces or just a single space at the end

Working example: https://regex101.com/r/czanzG/1

enter image description here

If you want to allow a single space after the name, then you just remove the \s from the group

@([\w\-\.\']+\s?(?:[\w\-\.\']+)?)$

https://regex101.com/r/IgROrF/1

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use regex alternative. First alternative is accepting the space and second is your original regex.

/@(\w+ \w+|\w+)/g

Updated

I forgot to put $ (regex the end of string) and include some characters .-'

/@([\w\-.']+ [\w\-.']+|[\w\-.']+)$/g

Example : https://regex101.com/r/LT7I1M/1

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