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

124
Visualizações
How to match numbers followed by any combination of special characters and spaces using regex

I am a newbie with regex and I am having trouble trying to match some parts of a string so I can remove it from a piece of entered text. I want to match digits followed by a sequence of a combination of any special characters + spaces. There could also be non Latin characters that should not be removed inside the sequence (for example Ñ).

for example inputted it may look like:

11@- &-.text
11 $ab*cÑ .somewords123

outputted I would expect

text
abcÑsomewrods123

I am using javascript replaceall method with regex to find it. So far I have something basic like this regex

.replaceAll(/\d+(\@|\s)+(\-|\$)+(\s|\&)+(\&)+(\-)+(\.)/g, '');

Is there a way to write this more efficiently so that it captures any special characters since the text can contain more different special chars than in the examples? Or is this a situation better handled with pure JS?

Thanks in advance for your help.

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

0

You should ether have blacklist of what you calling special characters, or whitelist of the allowed characters.

for blacklist it gonna look like:

const blacklist = "!@#$%^&*()_+.";

const exampleInputs = [
    "te&*st+_1.",
    "te%%st^*2",
    "t@@@es*(*(*t3"
 ];
 
function removeSpecialChars(str) {
  const reg = new RegExp(`[${blacklist}]`, "g");
  return str.replace(reg, "");
}

exampleInputs.forEach(input => console.log(removeSpecialChars(input)));

for whitelist it gonna looks like:

const whitelist = "0-9a-zA-Z";

const exampleInputs = [
    "te&*st+_1.",
    "te%%st^*2",
    "t@@@es*(*(*t3"
 ];
 
function removeSpecialChars(str) {
  const reg = new RegExp(`[^${whitelist}]`, "g");
  return str.replace(reg, "");
}

exampleInputs.forEach(input => console.log(removeSpecialChars(input)));

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