我有以下代码:
searchSt ="测试", searchEd ="在&#34 ;;
var html = body.clone()
.children()
.remove()
.end()
.text();
alert(searchSt + "|" + searchEd);
var patt = searchSt + "(.*)" + searchEd;
var result = html.match(patt);
alert(result[0]); //returns null
虽然我确定这些词是存在的,但这会返回null: HTML:
Hello World
Test Write SomeThing here is Words do it. hit in me
有什么问题?
答案 0 :(得分:1)
如果您使用的是JQuery,则代码应如下所示:
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<body>
Hello World
Test Write SomeThing here is Words do it. hit in me
</body>
</html>
<script type="text/javascript">
var searchSt = "Test", searchEd = " in";
var html = $('body').clone().children().remove().end().text();
alert(searchSt+"|"+searchEd);
var patt = searchSt+"(.*)"+searchEd;
var result = html.match(patt);
alert(result[0]);//returns null
</script>
确保脚本位于body标签下方。如果它高于身体标签,则无法找到。而且我也改变了如何获得身体$('body')的方式。
正则表达式没有问题
答案 1 :(得分:0)
在RegExp构造函数中放置一个字符串正则表达式(如果需要,可以使用字符):
var re = new RegExp(searchSt + "(.*)" + searchEd); // => /Test(.*)in/
倒杆:
var re = new RegExp("\\d"); // => /\d/
答案 2 :(得分:0)
它看起来不像是一个正则表达式问题。可能还有别的东西。
在这里测试http://ideone.com/nNwz3
var html = "Hello World\
Test Write SomeThing here is Words do it. hit in me";
var searchSt = "Test", searchEd = " in";
var patt = searchSt+"(.*)"+searchEd;
var result = html.match(patt);
print ("'" + result[0] + "'");
print ("'" + result[1] + "'");
输出
'Test Write SomeThing here is Words do it. hit in'
' Write SomeThing here is Words do it. hit'