Suppose I have a sentence: "This is a test sentence." I want the user to be able to select subsections of the text, but without punctuation or incomplete words.
So, the "test offer." should become a "test offer", and "est sentenc" should also become a "test offer"
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 fine.
EDIT for Bender:
Well, that's almost it. I have more than 50 thousand sentences, and the userโs choice is variable, so I need to do something like this:
var selection = getSelectedText(); var exp = new RegExp("\\w*" + selection + "\\w+"); text.match(exp);
However, this will not match if the user selects a โtest offerโ, which is more likely.
javascript jquery
Peter R
source share