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

229
Visualizações
Regex for Alphanumeric Values and not two consecutive underscores underscore

I have a requirement in javascript to write a regex for the following condition

The Custom Metadata Record MasterLabel field can only contain underscores and alphanumeric characters. It must be unique, begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.

If we leave out the unique part since we have to check it with the system records i need a regex for testing the other conditions mentioned above.

Here is an expression ive built so far but it doesnt seem to work. I am new to the regular expressions so any help would be appreciated.

/^[a-zA-Z]([a-zA-Z0-9][^ ][^__])[^_]*$/
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The string has all the required properties if and only if it matches the following regular expression:

/^[a-z][a-z\d]*(?:_[a-z\d]+)*$/i

Demo

The regular expression can be broken down as follows.

/
^           # match beginning of string
[a-z]       # match a letter
[a-z\d]*    # match one or more alphanumeric characters
(?:         # begin non-capture group
   _        # match an underscore
  [a-z\d]+  # match one or more alphanumeric characters
)*          # end non-capture group and execute it zero or more times
$           # match end of string
/i          # specify matches of letters to be case-indifferent

Notice that the way I prevent the presence of two underscores in a row has the fortuitous side-effect of ensuring that the last character in the string is not an underscore (a freebie!).

about 4 years ago · Juan Pablo Isaza Relatório

0

You might be looking for

^(?!_|\d)(?!.*__)(?!.*_$)\w+$

See a demo on regex101.com.


In JavaScript:

const regex = /^(?!_)(?!.*__)(?!.*_$)\w+$/gm;
const str = `_
A
abcd
_
123
some_spaces 
not_any_spaces__
1test34
correct_thing`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`${match}`);
    });
}

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