Wolly Wombat
Wolly Wombat

Reputation: 117

Equivalent of PHP preg_match, substr in javaScript

i'm makeing a tinyMce plugin that change word/letter space between highlighted text. My fn() looks like this:

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

Now i need to find closest tag before select start position (if exist) and get style values of "word-spacing" and "letter-spacing". Also i need to get rid of any inside of selection, but only tags not the text, knowing that span tags can have different styles so simple str.replace won't work.

I know that there is a built-in plugin for that, but i need to do it outside of tinyMce iframe, and customize it.

Any sugestions?

Upvotes: 1

Views: 487

Answers (1)

Ian
Ian

Reputation: 25366

JS regexes are available and do exactly what you want with preg_match.

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

Substring in javascript is str.substr(start, length)

Upvotes: 3

Related Questions