正则表达式转义字符

时间:2018-11-29 19:09:36

标签: javascript regex

我很难让表达式生效。如果没有终止行终止符和斜杠,我就可以正常工作。

The Server Job .([^\\]*).([^\\]*).([^\\]*) has Completed Abnormally on the Server Master (.*)\.\n\nJOB ID: (\d+)\n\nFollow Batch abend and escalation procedures. https://regex101.com/r/5TrEBB/1

示例文字:

The Server Job \FACET\Batch\SERV1254 SERVER IGX OUTPUT FILES CREATION has Completed Abnormally on the Server Master SER130P.

JOB ID: 10560914

Follow Batch abend and escalation procedures.

但是,我需要它来处理两个示例文本,如下所示:

The Server Job \\FACET\\Batch\\SERV1254 SERVER IGX OUTPUT FILES CREATION has Completed Abnormally on the Server Master SER130P.\n\nJOB ID: 10560914\n\nFollow Batch abend and escalation procedures.

请注意转义\和\ n。基本上是一个或两个斜线以及换行符或\ n \ n。

1 个答案:

答案 0 :(得分:0)

以下两者均可使用:

The Server Job (?:\\|\\\\)([^\\]*)(?:\\|\\\\)([^\\]*)(?:\\|\\\\)([^\\]*) has Completed Abnormally on the Server Master (.*)\.(?:\n\n|\\n\\n)JOB ID: (\d+)(?:\n\n|\\n\\n)Follow Batch abend and escalation procedures.

只要您有可能的转义/非转义字符,请使用“或”组并避免捕获它。