Div “contenteditable” : get and delete word preceding caret

后端 未结 2 1395
感动是毒
感动是毒 2021-01-14 00:22

Thanks to this question and answer posted by Tim Down, I made a function to get the word preceding caret in a \"contenteditable\" div.

Here\'s a fiddle, and here\'s

2条回答
  •  遥遥无期
    2021-01-14 01:17

    You could try using index property of match()

    var lastWordPosition = preceding.match(/(?:\s|^)([\S]+)$/i).index;
    

    Then you can do a substring(0, lastWordPosition) or whatever you want…

提交回复
热议问题