Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 4 of 4 FirstFirst ... 234
Results 46 to 47 of 47
  1. #46
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    LMAO! Kudos to BOTH of you! Much better than my attempt at humor.
    Be yourself. No one else is as qualified.

  2. #47
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,699 Times in 2,675 Posts
    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):-

    <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.
    aardvarks whistle. dogs jump when they are happy
    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")}
    Last edited by Philip M; 02-01-2013 at 04:34 PM. Reason: Typo noticed

    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.

Page 4 of 4 FirstFirst ... 234

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts