I am at a loss to understand why you are faffing around with incomprehensible and dubious regexes which look like an expletive in Klingon when the code I have supplied correctly finds all the examples you have given. Regardless of whether the sentence starts with a capital letter or not.
Here it is again (slighty improved):-
Code:
<html>
<head>
</head>
<body>
<div id = "mytext">Lorem ipsum dolor sit amet, consectetur adipiscing elit? Etiam ipsum leo, scelerisque at dapibus ac, consectetur vel ipsum! Morbi et metus ut diam molestie ullamcorper. Suspendisse rutrum semper semper. Donec volutpat neque in lorem tempus scelerisque. Curabitur dignissim rhoncus quam ac suscipit. Donec viverra quam lobortis neque porta a sagittis urna tristique. Suspendisse nec lacus nisi. Pellentesque fermentum massa sit amet magna hendrerit vestibulum. Sed elit libero, scelerisque eu eleifend ut, interdum gravida nunc? Etiam ut nisi sapien, et tempus sem. Nam vel mi est. Mauris congue felis ut ante bibendum vehicula. Nullam nec sapien arcu, eget cursus lorem. Donec blandit, dolor tristique ornare dictum, arcu sapien vulputate dolor, et placerat risus odio ut magna. Ut magna mauris, pellentesque at ultricies vitae, fermentum vitae dolor.
<br><br>
aardvarks whistle. dogs jump when they are happy
</div>
<br><br>
Enter word or phrase to find <input type = "text" id = "theword" onblur = "findit()">
<script type = "text/javascript">
var text = document.getElementById("mytext").innerHTML;
var ts = text.split(/\.|;|\?|!/); // split at period or semi-colon or ? or !
//var ts = text.split("."); // split at period only
alert (ts); // for testing
function findit() {
var intext = false;
var tofind = document.getElementById("theword").value;
//var regexp = new RegExp(tofind, 'gi'); // setting regex case insensitive and global, finds embedded words
var regexp = new RegExp("\\b" + tofind + "\\b", 'gi'); // setting regex case insensitive and global, finds whole words/phrases only
for (var i=0; i < ts.length; i++) {
ts[i] = ts[i].replace(/^\s+|\s+$/g,""); // strip leading and trailing spaces
if (regexp.test(ts[i])) {
intext = true;
alert ("The word or phrase " + tofind + " was found in the sentence:- " + "\n" + ts[i] + ".");
}
}
if (!intext) {alert ("The word or phrase " + tofind + " was not found")}
}
</script>
</body>
</html>