I am intrested in finding proper noun in a string but not if they are the first word in the sentence or the word is the fist word in the string. So for example I want to match the words Lipsum and Adipiscing and NOT “Lorem” because it is the first word and not “Consectetur” because it is the first word in the sentence.
Lorem Lipsum DOLOR ament blah blah. Consectetur Adipiscing elit ament blah
I did something like this
var txt = $.trim($('#wrapper').text());
var newVal = '';
txt = txt.split(' ');
var rg= new RegExp("^[A-Z][a-z]{2,}");
for(var i=0; i < txt.length; i++) {
if(txt[i].search(rg) === -1) {
newVal += txt[i] + " ";
}
else {
if (i == 0) {
newVal += txt[i] + " ";
}else{
newVal += "<span class='highlight'>"+txt[i]+"</span> ";
}
}
}
$('#wrapper').html(newVal);
this works and highlights the proper nouns but it also highlights the first word of the sentence. test at http://jsfiddle.net/ariyo/SU32g/
Any help would be appreciated.
I would do it about like this:
http://jsfiddle.net/aeCrG/3/