Home > Enterprise >  Replacing a JavaScript string in the last occurrence of two tildes ~~
Replacing a JavaScript string in the last occurrence of two tildes ~~

Time:01-26

I have a String which looks like this:

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~~

What I'm struggling with is the following:

Between the last occurrence of tildes (~~) we have Comment 2. I want to replace that value with another value, for instance Comment 3.

My solution would be this: https://jsfiddle.net/13wrpa2b/

alert(str[19]) // Comment 2

str[19] = "Comment 3"; 

let concat = str.join('~~');
alert(concat);

The problem with that is that I don't always know the exact length of the log and the comment does not necessarily have to be in the 19th place. But I do know that it will always be between the last occurrence of tildes ~~.

How can I achieve that?

CodePudding user response:

Because there are two tildes at the end of the string the array will look like [..., "Comment 2", ""]. Therefore the item is in the second to last position so instead of using 19 you can use 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);

CodePudding user response:

You can try the following:

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);

CodePudding user response:

You could replace with a regular expression.

const
    string = 'Mon Jan 24 2022 09:28:10 GMT 0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~\nMon Jan 24 2022 09:28:20 GMT 0100 (Mitteleuropäische Normalzeit)~~Admin_SA, SharePoint (i:0#.w|xespsa)~~00-Entwurf~~15-Indirekter Einkauf~~~~\nTue 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~~\nTue 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~~',
    result = string.replace(/(?<=~~)[^~] (?=~~$)/gm, 'Comment3');

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

You can use the regex: /(?<=~~)[^~] (?=~~$)/gm

This creates a lookbehind and lookahead for words at the end of the line wrapped by two tildes ~~

(If you want the last in the whole string instead of last in the line, remove the gm at the end)

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'))


Of course, you can even choose the replacement based on the selection with:

str.replace(regex, selection => selection == 'Comment 2' ? 'Comment 3' : 'Comment 4')

Extending this with the case of incrementing the number (in case you need to):

str.replace(regex, selection => selection.replace(/\d /, n =>  n 1))

CodePudding user response:

In sake of simplicity you could try:

str.replace(/~~Comment 2~~$/, "~~Comment 3~~");
  •  Tags:  
  • Related