2014-02-11 84 views
0

我想在用户键入tinymce时为tinymce中的所有单词设置一个新类。我还没有达到完美的解决方案。我到目前为止尝试的代码如下。我想在我的网站上创建#标记功能(如twitter和facebook)。如何设置所有单词的类以tinymce中的“#”开头?

注意:要添加的类别,这个词必须在标签内的包裹用新的类名

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); 
       } 
      }); 
     } 
    } 
+0

“单词”的语法是什么?它是不是'[a-zA-Z0-9_]'?还是你需要Unicode支持? – nhahtdh

+0

我会先单独构建'newstrHtml'。然后调用'ed'方法(setContent ...)一次。 – Stephan

+0

@nhahtdh unicode支持#tag可能不需要。但所有其他文字可以来自不同的语言。 –

回答

0

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

(?<=\s)#\w+ 
+0

我收到错误。 Lookbehind在JavaScript中不受支持 – jasonscript

相关问题