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-092020y 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-092020Esperarí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:
Los límites de palabras no funcionan aquí. Usar
/(?<!\S)(\S+)(?!\S)(?=.*(?<!\S)\1(?!\S))/gEXPLICACIÓ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