Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 03:56 AM   PM User | #46
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LMAO! Kudos to BOTH of you! Much better than my attempt at humor.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-01-2013, 07:29 AM   PM User | #47
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
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
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>
__________________

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; 02-01-2013 at 04:34 PM.. Reason: Typo noticed
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:21 PM.


Advertisement
Log in to turn off these ads.