我有一個看起來像這樣的字串:
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 位。但我知道它總是在波浪線的最后一次出現之間~~。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
因為字串末尾有兩個波浪號,所以陣列看起來像[..., "Comment 2", ""]. 因此,該專案位于倒數第二個位置,因此19您可以使用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);
uj5u.com熱心網友回復:
您可以嘗試以下方法:
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);
uj5u.com熱心網友回復:
您可以用正則運算式替換。
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; }
uj5u.com熱心網友回復:
您可以使用正則運算式:/(?<=~~)[^~] (?=~~$)/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))
uj5u.com熱心網友回復:
為簡單起見,您可以嘗試:
str.replace(/~~Comment 2~~$/, "~~Comment 3~~");
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/421447.html
標籤:
