Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

316
Views
Javascript RegEx: /(\b\S+\b)(?=.*\b\1\b)/ para eliminar la palabra duplicada falla en algún momento cuando (-,/) están presentes

Revisé varias publicaciones relacionadas con la eliminación de palabras duplicadas (en mi caso, palabra significa una subcadena separada por un espacio) en javascript en una Cadena. El siguiente RegEx: /(\b\S+\b)(?=.*\b\1\b)/g está entre los que encontré en Internet que coincide con casi todos los casos, pero produce algunos desajustes que estoy sin poder averiguar por qué. Por ejemplo, elimina algunos caracteres como: , /- en situaciones en las que es parte de la cadena (todavía no ha llegado a un espacio en blanco). Supongo que tiene que ser con el metacarácter de límite de palabra \b pero no puedo encontrar una solución para eso.

Por ejemplo, tengo las siguientes muestras de cadenas:

 123-1 123-2 test-1 test-1 w/e 10/04/20 Company w/e 09/06/20 083020-090620 a/b 01/01 test_1 test_2 a/ba/b Inv 50049 50049 Inv 50195 PrjPAN02 Inv 51360-1, 51366-7; 51372 Inv 51360-1, 51366-7; 372 PrjPAN02 Inv 51360-1, 51366-7; 51372 51372 Inv 513601, 51366-7; 372 PrjPAN02 55009, 55017, 55022 55001, 55022, 55025 55254, 61 55246,66,69 55733, 41, 44 55727, 45,48 57269, 71,74,75, 57354 57266, 73 57437, 38, 41, 43 57434, 40 w/e 09/20/20 091320-092020

y genera la siguiente salida. Puedes probarlo aquí: Regex101

 1232 test-1 we 1004/20 Company we 0906/20 083020-090620 ab /01 test_1 test_2 a/b 50049 Inv 50195 PrjPAN02 , ; 51372 Inv 513601, 51366-7; 372 PrjPAN02 513601, ; 51372 Inv 513601, 51366-7; 372 PrjPAN02 55009, 55017, 55001, 55022, 55025 55254, 61 5524666,69 55733, 41, 44 55727, 45,48 57269, 7174,75, 57354 57266, 73 57437, 38, 41, 43 57434, 40 we 09/20 091320-092020

Esperaría el siguiente resultado:

 123-1 123-2 test-1 w/e 10/04/20 Company w/e 09/06/20 083020-090620 a/b 01/01 test_1 test_2 a/b 50049 Inv 50195 PrjPAN02 51372 Inv 51360-1, 51366-7; 372 PrjPAN02 51360-1, 51372 Inv 513601, 51366-7; 372 PrjPAN02 55009, 55017, 55022 55001, 55022, 55025 55254, 61 55246,66,69 55733, 41, 44 55727, 45,48 57269, 71,74,75, 57354 57266, 73 57437, 38, 41, 43 57434, 40 w/e 09/20/20 091320-092020

Esperaría que se eliminen todas las cadenas repetidas delimitadas por espacios, pero ReEx elimina la barra inclinada ( / ), el guión ( - ) y la coma ( , ) en algunos casos dentro de cadenas delimitadas por espacios.

Revisé la siguiente pregunta similar para tratar de encontrar expresiones regulares que coincidieran con todos los casos:

  1. Javascript RegExp + Límites de palabras + caracteres Unicode
  2. Eliminar palabras duplicadas en una cadena usando Regex JS [duplicado]
  3. Expresión regular para encontrar y eliminar palabras duplicadas
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Los límites de palabras no funcionan aquí. Usar

 /(?<!\S)(\S+)(?!\S)(?=.*(?<!\S)\1(?!\S))/g

EXPLICACIÓN

 -------------------------------------------------------------------------------- (?<! look behind to see if there is not: -------------------------------------------------------------------------------- \S non-whitespace (all but \n, \r, \t, \f, and " ") -------------------------------------------------------------------------------- ) end of look-behind -------------------------------------------------------------------------------- ( group and capture to \1: -------------------------------------------------------------------------------- \S+ non-whitespace (all but \n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- ) end of \1 -------------------------------------------------------------------------------- (?! look ahead to see if there is not: -------------------------------------------------------------------------------- \S non-whitespace (all but \n, \r, \t, \f, and " ") -------------------------------------------------------------------------------- ) end of look-ahead -------------------------------------------------------------------------------- (?= look ahead to see if there is: -------------------------------------------------------------------------------- .* any character except \n (0 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- (?<! look behind to see if there is not: -------------------------------------------------------------------------------- \S non-whitespace (all but \n, \r, \t, \f, and " ") -------------------------------------------------------------------------------- ) end of look-behind -------------------------------------------------------------------------------- \1 what was matched by capture \1 -------------------------------------------------------------------------------- (?! look ahead to see if there is not: -------------------------------------------------------------------------------- \S non-whitespace (all but \n, \r, \t, \f, and " ") -------------------------------------------------------------------------------- ) end of look-ahead -------------------------------------------------------------------------------- ) end of look-ahead
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!