Reputation: 8176
I have the following code:
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
This returns null although I am sure the words are there: html:
Hello World
Test Write SomeThing here is Words do it. hit in me
What is wrong?
Upvotes: 0
Views: 91
Reputation: 942
Put a string regular expression inside the RegExp constructor (espacing the characters, if needed):
var re = new RegExp(searchSt + "(.*)" + searchEd); // => /Test(.*)in/
With inverted bars:
var re = new RegExp("\\d"); // => /\d/
Upvotes: 0
Reputation: 51
If you are using JQuery the code should be the following:
<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>
Make sure that script is placed below the body tag. If it's above than the body tag can't be found. And also I changed the way how to get body $('body').
There is no problem with regex
Upvotes: 1
Reputation:
It doesn't look like its a regex problem. Probably something else.
Test here 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] + "'");
output
'Test Write SomeThing here is Words do it. hit in'
' Write SomeThing here is Words do it. hit'
Upvotes: 0