如何为所有单词设置一个类以tinymce中的“#”开头?

时间:2014-02-11 10:30:25

标签: javascript jquery regex tinymce

我希望在用户输入tinymce时为tinymce中的所有单词设置一个新类。我还没有达到完美的解决方案。我到目前为止尝试的代码如下。我想在我的网站上创建#tagging功能(比如在twitter和facebook中)。

注意:要添加该类,必须将该单词包装在具有新类名

的标记内
function createArticleKeyUp(ed,e)//ed is the tinymce editor, e is the keyup event
    {
        if(e.keyCode==32){//only check for the space key

            var strHtml = ed.getContent();//get content of tinymce ie html content
            var str=$(strHtml).text();//html stripped out
            str=str.replace(" "," ");
            str=str.replace(/\n/g, "");
            $.each(str.split(" "),function(index,val){
                var isStartsWithHash=val.indexOf("#") == 0;
                if(isStartsWithHash)
                {
                    var newstrHtml=strHtml.replace(val,"<a class='newClassToset'>"+val+"</a>");

                    ed.setContent(newstrHtml);
                    ed.selection.select(ed.getBody(), true); // this is to set cursor at the end
                    ed.selection.collapse(false);
                }
            });
        }
    } 

1 个答案:

答案 0 :(得分:0)

所有以#符号开头的单词?

(?<=\s)#\w+