[英]Replacing a JavaScript string in the last occurrence of two tildes ~~
我有一个看起来像这样的字符串:
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~~
我正在努力解决的问题如下:
在最后一次出现的波浪号 (~~) 之间,我们有Comment 2
。 我想用另一个值替换该值,例如Comment 3
。
我的解决方案是: https://jsfiddle.net/13wrpa2b/
alert(str[19]) // Comment 2
str[19] = "Comment 3";
let concat = str.join('~~');
alert(concat);
问题是我并不总是知道日志的确切长度,并且评论不一定必须在第 19 位。 但我知道它总是在波浪线的最后一次出现之间~~。
我怎样才能做到这一点?
因为字符串末尾有两个波浪号,所以数组看起来像[..., "Comment 2", ""]
。 因此,该项目位于倒数第二个 position 因此您可以使用str.length - 2
而不是使用19
。
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);
您可以尝试以下方法:
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);
您可以使用正则表达式: /(?<=~~)[^~]+(?=~~$)/gm
这会在由两个波浪号包裹的行尾创建一个后向和前向的单词~~
(如果您想要整个字符串中的最后一个而不是行中的最后一个,请删除最后的gm
)
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'))
当然,您甚至可以根据选择来选择替换:
str.replace(regex, selection => selection == 'Comment 2' ? 'Comment 3' : 'Comment 4')
在增加数字的情况下扩展它(如果需要):
str.replace(regex, selection => selection.replace(/\d+/, n => +n+1))
您可以用正则表达式替换。
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; }
为简单起见,您可以尝试:
str.replace(/~~Comment 2~~$/, "~~Comment 3~~");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.