2013-03-29 169 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

那就是我需要的,謝謝 –