Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > RegExp to match sentences in a paragraph

Thread Tools

RegExp to match sentences in a paragraph

Posts: n/a
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[i] = trimString(re_array[i]);
pattern[i] = new RegExp (re_array[i],'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 class='your_answer'>");
document.write(re_array[n]+". ");
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.

Reply With Quote

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why choose a paragraph element for a paragraph? dorayme HTML 112 03-30-2009 10:56 PM
String#match vs. Regexp#match - confused Old Echo Ruby 1 09-04-2008 06:11 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
splitting paragraph into sentences Sandman Perl Misc 5 08-02-2004 07:06 PM
regexp to list all sentences and sub sentences, with overlapping? Tony Perl 4 11-27-2003 01:38 PM