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. Advertising

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,189
    Andy De Petter
    Nov 27, 2003
  2. dorayme
    Replies:
    112
    Views:
    2,835
    dorayme
    Mar 30, 2009
  3. Mikel Lindsaar
    Replies:
    0
    Views:
    546
    Mikel Lindsaar
    Mar 31, 2008
  4. Old Echo
    Replies:
    1
    Views:
    200
    Adam Shelly
    Sep 4, 2008
  5. Sandman

    splitting paragraph into sentences

    Sandman, Aug 2, 2004, in forum: Perl Misc
    Replies:
    5
    Views:
    461
    Anno Siegel
    Aug 2, 2004
Loading...

Share This Page