Java 服务器错误消息的正确正则表达式

Java 服务器错误消息的正确正则表达式,java,javascript,regex,error-handling,alert,Java,Javascript,Regex,Error Handling,Alert,我正在进行一个Ajax调用,并且从服务器收到一个错误 现在的问题是我得到了下面的信息 HTTP Status 756 - Error while processing the request. -------------------------------------------------------------------------------- type Status report message Error while processing the request. descr

我正在进行一个Ajax调用,并且从服务器收到一个错误

现在的问题是我得到了下面的信息

HTTP Status 756 - Error while processing the request.

--------------------------------------------------------------------------------

type Status report

message Error while processing the request.

description Cannot find message associated with key http.756
我只想从完整的错误报告中获取错误消息,而不是上面的所有文本。我该怎么做

但实际的反应是

<html><head><title>Apache Tomcat/5.0.28 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 756 - Error while processing the request.</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Error while processing the request.</u></p><p><b>description</b> <u>Cannot find message associam<D‡üñÔE(1@@ähttp.756</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/5.0.28</h3></body></html>​

Apache Tomcat/5.0.28-错误报告HTTP状态756-处理请求时出错。
在处理请求时键入状态报告。消息错误。说明找不到消息关联当您返回
响应
HTML时,您可以像这样抓取消息

var div = document.createElement("div");

div.innerHTML = response;

var errorMsg = [].filter.call(div.getElementsByTagName("b"), function(b) {
    return b.textContent == "message";
})[0].nextElementSibling.textContent || "Unknown error";


如果只是文字

这将提取第一行
-
后面的文本。如果找不到匹配项,它将返回“未知错误”

如果相反,您希望匹配下面的
消息

var errorMsg = (response.match(/^message (.+)$/m) || [])[1] || "Unknown error";

.

检查此工作示例:


我通过下面的JavaScript代码得到了答案

var res = "Error Message : '";

var bonly = data.responseText.match(/<h1>(.*?)<\/h1>/);
if (bonly && (bonly.length > 1)) {
    res += bonly[1];
}
res += "'. Error Code : ";
res += data.status;
return res;
var res=“错误消息:”;
var bonly=data.responseText.match(/(.*)/);
如果(bonly&(bonly.length>1)){
res+=bonly[1];
}
res+=“”。错误代码:”;
res+=数据状态;
返回res;

捕捉
消息(.*)
不是更好吗?@hsz在OP的示例中,它们似乎是相同的东西。@hsz我也添加了一个关于如何做到这一点的示例:)@BhavikAmbani这是应该包含在原始答案中的东西。那么你能发布HTML吗?@alex上面的内容是什么?意思是你得到的结果是
HTML
格式。如果是,你能在问题中指定吗?请查看编辑后的问题,我想从中获取错误代码和错误消息JavaScript正则表达式不支持lookbehinds。
(?<=-\s).*
(?<=[0-9]\s-\s).*
(?<=<h1>).*(?=</h1>)
var res = "Error Message : '";

var bonly = data.responseText.match(/<h1>(.*?)<\/h1>/);
if (bonly && (bonly.length > 1)) {
    res += bonly[1];
}
res += "'. Error Code : ";
res += data.status;
return res;