我有一个字符串,它动态地来自另一个文档,如下所示;使用双引号的字符串正则表达式匹配失败
"<!DOCTYPE html>
<html dir="ltr"><head><title>Preview</title></head>
<body>
<p>test</p>
<p><img alt="" height="299" src="http://172.0.0.1/Administration/YDImages/cap.JPG" width="696"></p>
</body>
</html>"
我使用这个字符串如下;
var html = stringAbove;
var reg = html.match(/<body[^>]*>(.*)<\/body>/);
var newDocument = "<p>My new Texts and styles</p>"; //replace inside body with my new code
var newer = html.replace(reg[1],newDocument);
doc.write(newer);
我发现html.match
回报null
如果HTML变量中的字符串作为它的正上方,在调试时,看我怎么可以让开发者的工具,此正则表达式的工作,我已经改变了开头和结尾双引号的字符串单引号,所以它的工作。然后我将所有双引号改为单引号,并尝试正则表达式函数,但它不起作用。请帮助我正确使用这个正则表达式。
我建议不要使用正则表达式解析HTML [这里](HTTP:// stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)是[why](http://stackoverflow.com/questions/590747/using-regular-expressions-to -parse-html-why-not) – George
感谢您的信息,我会研究我能做些什么。 – ibubi