Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    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.

  2. #47
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 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):-


    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>
    Last edited by Philip M; 02-01-2013 at 05: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
  •