Lets say I have a sentence: "This is a test sentence." I want a user to be able to select subsections of the text, but without punctuation, or incomplete words.
So "test sentence." should become "test sentence" and "est sentenc" should also become "test sentence"
Here is my current code:
var getSelectedText = function() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString().trim();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
fyi: jQuery code is ok.
EDIT for Bender:
Ok this is almost there. I have over 50k sentences and the user selection is variable, so I'll have to do something like this:
var selection = getSelectedText();
var exp = new RegExp("\\w*" + selection + "\\w+");
text.match(exp);
However, this won't match if a user DOES select "test sentence" which is more likely than not.