View Single Post
Old 01-31-2013, 07:11 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Here's my suggestion:-

Code:
<html>
<head>
</head>
<body>

Enter word to find <input type = "text" id = "theword" onblur = "findit()">

<script type = "text/javascript">

var text = "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."

//var ts = text.split(/\.|;/);   // split at period or semi-colon
var ts = text.split(".");  // split at period only

function findit() {
var intext = false;
for (var i=0; i < ts.length; i++) {
var found = false;
var tofind = document.getElementById("theword").value;
var regexp = new RegExp(tofind, 'gi');	 // setting regex case insensitive and global
if (regexp.test(ts[i])) {
found = true;
intext = true;
}
if (found) {alert ("The word " + tofind + " was found in the sentence:- " + "\n" + ts[i])}
}
if (!intext) {alert ("The word " + tofind  + " was not found.")}

}

</script>

</body>
</html>
Christians only have one spouse. This is called monotony.
- Pupil's answer to Catholic Elementary School test.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-31-2013 at 07:21 PM..
Philip M is offline   Reply With Quote