Thursday, January 27, 2011

Filter HTML/XML tags from a text

function filterHtmlTags(source) {
var result=source.replace(/(<([^>]+)>)/ig,"");
result=trim(result, "\n");
return result;
}

//function to trim of leading and trailing spaces
function trim(str, chars) {
return ltrim(rtrim(str, chars), chars);
}

//function to trim of white spaces on the left
function ltrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

//function to trim of white spaces on the right
function rtrim(str, chars) {
chars = chars || "\\s";
return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

No comments:

Post a Comment