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

121
Visualizações
RegEx to match exactly one occurrence of character at the end of string in Javascript

I have a string and want to match pattern which has either s or m at the end of string having only one occurrence. I haven't used regex much and unable to find any answers.
E.g. If the string is 121ss. It should return false. 121s and 121m should return true. 121ms should also return false. It is also case sensitive so 121M or 121H won't do.
The pattern I tried using is /[mh]{1}$/

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

0

If "m" and "s" must be preceded by at least one or more numbers \d+ use:

/\d+[ms]$/

Demo on Regex101

1m      // matches
123m    // matches
1s      // matches
123s    // matches
121ss
121ms
1M
1S
123MS
xyzn
xyzs

PS: if you already use [ms] (meaning "m or s") then the {1} is unnecessary since it matches only one option.

about 4 years ago · Juan Pablo Isaza Relatório

0

The last character must be m or s, and the next to last character must not be either of them:

/[^ms][ms]$/

You can try this on RegExr, which is a playground with very helpful hints

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