Tengo una cadena que se ve así:
Mon Jan 24 2022 09:28:10 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~ Mon Jan 24 2022 09:28:20 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~ Tue Jan 25 2022 20:51:17 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~15-Indirekter Einkauf~~15-Indirekter Einkauf~~Comment 1~~ Tue Jan 25 2022 20:58:34 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~15-Indirekter Einkauf~~15-Indirekter Einkauf~~Comment 2~~Con lo que estoy luchando es con lo siguiente:
Entre la última aparición de tildes (~~) tenemos el Comment 2 . Quiero reemplazar ese valor con otro valor, por ejemplo, Comment 3 .
Mi solución sería esta: https://jsfiddle.net/13wrpa2b/
alert(str[19]) // Comment 2 str[19] = "Comment 3"; let concat = str.join('~~'); alert(concat);El problema con eso es que no siempre sé la longitud exacta del registro y el comentario no necesariamente tiene que estar en el lugar 19. Pero sí sé que siempre será entre la última aparición de tildes ~~.
¿Cómo puedo lograr eso?
Debido a que hay dos tildes al final de la cadena, la matriz se verá como [..., "Comment 2", ""] . Por lo tanto, el elemento está en la penúltima posición, por lo que en lugar de usar 19 , puede usar str.length - 2 .
let str = 'Mon Jan 24 2022 09:28:10 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~ Mon Jan 24 2022 09:28:20 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~ Tue Jan 25 2022 20:51:17 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~15-Indirekter Einkauf~~15-Indirekter Einkauf~~Comment 1~~ Tue Jan 25 2022 20:58:34 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~15-Indirekter Einkauf~~15-Indirekter Einkauf~~Comment 2~~'.split('~~') console.log(str[str.length - 2]) // Comment 2 str[str.length - 2] = "Comment 3"; let concat = str.join('~~'); console.log(concat);Puedes intentar lo siguiente:
const arr = logentry.split('~~'); arr[arr.length-2] = 'My custom stuff'; // this is now the last block in the line const updatedLine = arr.join('~~')); alert(updatedLine);Puede usar la expresión regular: /(?<=~~)[^~]+(?=~~$)/gm
Esto crea una mirada hacia atrás y hacia adelante para las palabras al final de la línea envuelta por dos tildes ~~
(Si desea el último en toda la cadena en lugar del último en la línea, elimine el gm al final)
const regex = /(?<=~~)[^~]+(?=~~$)/gm; const str = ` log: Mon Jan 24 2022 09:28:10 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~ Mon Jan 24 2022 09:28:20 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~ Tue Jan 25 2022 20:51:17 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~15-Indirekter Einkauf~~15-Indirekter Einkauf~~Comment 1~~ Tue Jan 25 2022 20:58:34 GMT+0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~15-Indirekter Einkauf~~15-Indirekter Einkauf~~Comment 2~~ `; console.log(str.match(regex)) console.log(str.replace(regex, 'replacement'))Por supuesto, incluso puede elegir el reemplazo en función de la selección con:
str.replace(regex, selection => selection == 'Comment 2' ? 'Comment 3' : 'Comment 4')Extendiendo esto con el caso de incrementar el número (en caso de que lo necesite):
str.replace(regex, selection => selection.replace(/\d+/, n => +n+1))