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

403
Vistas
Exact match multiple words in regex (datatables)

I'm trying to create a regex search in Datatables which matches a user who belongs to all selected groups.

Example:

User A belongs to Users, Other, Other Users

User B belongs to Users, Other

If Users and Other Users are selected as filters, only user A should show in the table. The problem I'm having is that both users are showing when these filters are selected. I don't think the regex is matching exact strings and, after looking through multiple other answers, I don't seem to be able to get it to do so.

My solution:

if (!this.items.length) {
    table.column(i).search('');
} else {
    let regex = '^';

    this.items.forEach(v => regex += `(?=.*\\b${v}\\b)`);

    regex += '.*$'

    table.column(i).search(regex, true, false, true)
}

Which results in: ^(?=.*\bUsers\b)(?=.*\bOther Users\b).*$

However, the user belonging to Users,Other is still being returned.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can enforce a comma or start/end of string check before and after each of your search term:

this.items.forEach(v => regex += "(?=.*(?:[^,]|^)${v}(?![^,]))");

Or, if the JavaScript environment supports lookbehinds:

this.items.forEach(v => regex += "(?=.*(?<![^,])${v}(?![^,]))");

The (?:[^,]|^) / (?<![^,]) (equal to (?<=,|^)) part requires start of string position or a comma right before your search term and the (?![^,]) negative lookahead requires a comma or end of string immediately to the right of the current position ((?![^,]) is equal to (?=,|$) positive lookahead).

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