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

227
Visualizações
Remove specific words from a string in an efficient way

I am trying to strip out a couple of matching words from an array of string. Below is the array containing the string

let string = ["select from table order by asc limit 10 no binding"]

I am trying to get rid of any that has order by and limit and its value and preserving the remaining. I have tried the following but none of them are elegant/efficient.

let splitString = string.split(' ');
let str1 = 'limit';
let str2 = 'order';
let str3 = 'by';
let str4 = 'asc';
let str5 = '10';

splitString = splitString.filter(x => x !== str1);
splitString = splitString.filter(x => x !== str2);
splitString = splitString.filter(x => x !== str3);
splitString = splitString.filter(x => x !== str4);
splitString = splitString.filter(x => x !== str5);

Is there a proper way of getting rid of those words from the string? TIA

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

0

Make an array or Set of the strings you want to remove, then filter by whether the word being iterated over is in the Set.

const input = ["select from table order by asc limit 10 no binding"]
const wordsToExclude = new Set(['limit', 'order', 'by', 'asc', '10']);
const words = input[0].split(' ').filter(word => !wordsToExclude.has(word));
console.log(words);

If you don't actually want to remove all of those words, but only such a sequence, use a regular expression to match limit until you come across numbers.

const input = ["select from table order by asc limit 10 no binding"];
const result = input[0].replace(/order\b.*?\d+ /, '');
console.log(result);

If additional numbers can come in between the sequence, match limit \d+ at the end, rather than just \d+

const input = ["select from table order by 1 asc limit 33 no binding"];
const result = input[0].replace(/order\b.*?limit \d+ /, '');
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

Using filter and includes

const [string] = ["select from table order by asc limit 10 no binding"];
const exclude = ["limit", "order", "by", "asc", "10"];

const res = string.split(" ").filter(wd => !exclude.includes(wd));

console.log(res)

Alternatively using replaceAll

const [string] = ["select from table order by asc limit 10 no binding"];
const exclude = ['limit', 'order', 'by', 'asc', '10'];

const res = exclude.reduce((str, word) => 
  str.replaceAll(word, ''), string).replaceAll(/\s+/g, ' ');
  
console.log(res)

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