lundi 30 mars 2015

Javascript: text selection including special characters

I'm using this javascript code to grab the word immediately preceding the caret:



sel = window.getSelection()
var selectedRange = sel.getRangeAt(0);
sel.collapseToStart();
sel.modify("move", "backward", "word");
sel.modify("extend", "forward", "word");


This gets the word, even without trailing whitespace (ex. "word") but omits special characters on the end of the word (ex. instead of "word:" gets just "word").


If I include the colon inside of a word, like "wo:rd", this gets all 5 characters.


I really need to get trailing special characters, though. How can I do this?





Aucun commentaire:

Enregistrer un commentaire