﻿function Init() {
    document.getElementById("txtSource").focus();
}

function GetMatchesAndDisplayResult(sourceText, expression, matchAll, matchCase) {
    try {
        var result = "";
        var regExpOptions = "";
        if (matchAll)
            regExpOptions += "g";
        if (!matchCase)
            regExpOptions += "i";
        var re = new RegExp(expression, regExpOptions);
        var arrMatches;
        var done = false;
        var matchIndex = 0;
        var displayCharPosBase = 1;

        while ((arrMatches = re.exec(sourceText)) != null && !done) {
            matchIndex++;
            var charPosDisplay;
            if (isNaN(arrMatches.lastIndex) || (arrMatches.index == arrMatches.lastIndex - 1))
                charPosDisplay = "char " + (arrMatches.index + displayCharPosBase);
            else
                charPosDisplay = "chars " + (arrMatches.index + displayCharPosBase) + "-" + ((arrMatches.lastIndex - 1) + displayCharPosBase);
            result += "Match #" + matchIndex + " (" + charPosDisplay + "): " + arrMatches[0] + "\r\n";
            var subMatchIndex;
            if (arrMatches.length > 1) {
                for (subMatchIndex = 1; subMatchIndex < arrMatches.length; subMatchIndex++) {
                    result += "  * sub " + subMatchIndex + ": " + arrMatches[subMatchIndex] + "\r\n";
                }
            }
            if (!matchAll)
                done = true;
        }
        if (matchIndex == 0)
            result = "No matches."
        return result;
    }
    catch (ex) {
        return "Error: " + ex.description;
    }
} 