如何用JavaScript从邮件体中精准提取指定字符串DATALINER?
Got it, let's fix this extraction issue. The problem with your current code is that after splitting on /, the third element includes everything from DATALINER onwards (like the Msg:... part). Here are a couple of reliable ways to get just DATALINER:
Method 1: Split Twice (Simple & Straightforward)
Since DATALINER is immediately followed by a space and Msg:, we can split the string twice—first on /, then on whitespace—to isolate the target value:
var body = "1180328 101252 RPG0907 710494/PRODUCTION/DATALINER Msg: CI300R 18200 decimal-data error in field (C G S D F).Cause . . . . . : The RPG program CI300R in library OBJECT found a decimal-da"; var jobName = body.split('/')[2].split(' ')[0]; console.log(jobName); // Outputs: DATALINER
This works because splitting on / gives us DATALINER Msg: ... as the third element, then splitting that result on whitespace grabs the first token (exactly what we need).
Method 2: Regular Expression (Robust for Variable Formats)
If you want a more resilient solution (in case the log format has minor variations), use a regex to match the exact pattern around DATALINER:
var body = "1180328 101252 RPG0907 710494/PRODUCTION/DATALINER Msg: CI300R 18200 decimal-data error in field (C G S D F).Cause . . . . . : The RPG program CI300R in library OBJECT found a decimal-da"; const regexPattern = /\/([^\/\s]+)\sMsg:/; const matchResult = body.match(regexPattern); var jobName = matchResult ? matchResult[1] : null; // Fallback to null if no match console.log(jobName); // Outputs: DATALINER
The regex /\/([^\/\s]+)\sMsg:/ breaks down like this:
\/: Matches the forward slash beforeDATALINER([^\/\s]+): Captures one or more characters that are NOT a slash or whitespace (this is our targetDATALINER)\sMsg:: Matches the space andMsg:that come right after it
This ensures we only capture the value we want, even if there are other characters or slight shifts in the log structure.
内容的提问来源于stack exchange,提问作者Robert

