RegExp to match sentences in a paragraph

Discussion in 'Javascript' started by Ana, Aug 2, 2004.

  1. Ana

    Ana Guest

    I am trying to write a function that will take compare two paragraphs
    and make the sentences that dont match ta different color. I have an
    text box where the user will edit a paragraph and when I call the
    function I send the original paragraph as a parameter "str" and the
    user input is "re". My code (as ugly and inefficient as it may be)
    seems to work for everything but the case of when a user erases a word
    from the original sentence. It still finds the edited sentence in the
    paragraph so it doesnt change color. I am a javascript hack so the
    following code may be very sloppy.

    Here is the code:
    /***********************************

    function trimString (str) {
    str = this != window? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
    }

    function matchMe(re, str) {
    str = trimString(str);
    var re_array = re.split(".");
    var pattern = new Array();
    for (var i=0; i<re_array.length; i++)
    {
    re_array = trimString(re_array);
    pattern = new RegExp (re_array,'ig');
    }
    for (var n=0; n<(re_array.length-1); n++)
    {
    if(str.match(pattern[n])!= null){
    document.write("<span class='bodytext_w'>");
    document.write(re_array[n]+". ");
    document.write("</span>");
    }
    else{
    document.write("<span class='your_answer'>");
    document.write(re_array[n]+". ");
    document.write("</span>");
    }
    }
    return false;
    }

    ***************************************************************/
    Is there another approach that I can take that will capture the
    scenario where a user deltes a word from a sentence?

    Any information would be greatly appreciated.

    Ana
     
    Ana, Aug 2, 2004
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony
    Replies:
    4
    Views:
    2,370
    Andy De Petter
    Nov 27, 2003
  2. Guru Nathan via JavaKB.com

    Counting no.of sentences

    Guru Nathan via JavaKB.com, Feb 28, 2005, in forum: Java
    Replies:
    13
    Views:
    993
  3. dorayme
    Replies:
    112
    Views:
    3,571
    dorayme
    Mar 30, 2009
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    745
    Mikel Lindsaar
    Mar 31, 2008
  5. Old Echo
    Replies:
    1
    Views:
    298
    Adam Shelly
    Sep 4, 2008
  6. Joao Silva
    Replies:
    16
    Views:
    662
    7stud --
    Aug 21, 2009
  7. Sandman

    splitting paragraph into sentences

    Sandman, Aug 2, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    803
    Anno Siegel
    Aug 2, 2004
  8. Uldis  Bojars
    Replies:
    2
    Views:
    334
    Janwillem Borleffs
    Dec 17, 2006
Loading...