2013-03-29 170 views
1

我正在制作一个tinyMce插件,用于更改突出显示的文本之间的文字/字母空间。我的FN()看起来是这样的:PHP preg_match等效于javaScript中的substr

function wordsLettersSpace(words, letters) { 
    var inst = tinyMCE.activeEditor; 
    var currSelection = $.trim(inst.selection.getSel()); 

    if(currSelection != ""){ 
     var content = ""; 
     content = inst.selection.getContent({ 
      format: 'html' 
     }); 

     contentReplace = '<span style="word-spacing: '+words+'px; letter-spacing: '+letters+'px;">' + inst.selection.getContent({ 
      format: 'html' 
     }) + '</span>'; 

     inst.selection.setContent(content.replace(content, contentReplace)); 
     window.alert(content); 
    }else{ 
     Hyphenator.hyphenate(inst.getBody(), language); 
    } 
    tinyMceUpdate(); 
} 

现在,我需要找到之前选择起始位置最接近的标记(如果存在的话),并获得“字间距”和“字母间距”的样式值。此外,我需要摆脱任何内部的选择,但只有标签不是文字,因为知道span标签可以有不同的风格,所以简单的str.replace不会工作。

我知道有一个内置的插件,但我需要在tinyMce iframe之外做,并对其进行自定义。

任何消耗?

回答

3

JS正则表达式可用,并按照您想要的preg_match进行操作。

https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions

子字符串在JavaScript中是str.substr(start, length)

+0

+1,虽然是迂腐一下,在JS正则表达式引擎具有略微不同的能力,以一个在PHP。但是绝大多数情况下,'str.match(/ regex /)'和PHP中的'preg_match()'的功能大致相同。 – Spudley

+0

那就是我需要的,谢谢 –