Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda