I'm trying to create an editable div that regularly checks for any text that was typed that matches @text
(starts with @ and ends with a space).
So for example, if the user were to type @text more text here
, it would change the word that starts with @ and ends with a space to a link like <a href="#">@text</a> more text here
within the div.
I've started with JSFiddle, however, I can't get it working: http://jsfiddle.net/MpFXK/2/
HTML:
<div class="box" contenteditable="true"></div>
JS:
$(document).on('keyup', ".box", function() {
var text = $(this).text();
var regexp = /\B@([^\B ]+)/;
if (text.match(regexp)) {
text = text.replace(/\B@([^\B ]+)/, '<a href="#">/\B@([^\B ]+)/</a> ');
return $(this).html(text);
}
return false;
});
Please help!