相当于PHP中的preg_match, javaScript中的substr

Equivalent of PHP preg_match, substr in javaScript

本文关键字:中的 javaScript match substr PHP preg 相当于      更新时间:2023-09-26

我正在制作一个tinyMce插件,改变突出显示文本之间的单词/字母空间。我的fn()看起来像这样:

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();
}

现在我需要在选择开始位置(如果存在)之前找到最接近的标签,并获得"word-spacing"answers"letter-spacing"的样式值。我还需要摆脱任何内部的选择,但只有标签不是文本,知道跨度标签可以有不同的风格,所以简单的str.replace将不起作用。

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

有什么建议吗?

JS正则表达式是可用的,并且可以完全按照您的需要使用preg_match。

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

javascript中的子字符串是str.substr(start, length)