﻿function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function replaceAll(str, searchTerm, replaceWith, ignoreCase) {
    var regex = "/" + searchTerm + "/g";
    if (ignoreCase) {
        regex += "i";
    }
    return str.replace(eval(regex), replaceWith);
}

function htmlEncode(s) {
    if (s) {
        return $('<div/>').text(s).html();
    }
    return '';
}
