Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

replace espacial characteres from string

function convertHtmlToText() {
    var inputText = document.getElementById("input").value;
    var returnText = "" + inputText;

    //-- remove BR tags and replace them with line break
    returnText=returnText.replace(/<br>/gi, "
");
    returnText=returnText.replace(/<brs/>/gi, "
");
    returnText=returnText.replace(/<br/>/gi, "
");

    //-- remove P and A tags but preserve what's inside of them
    returnText=returnText.replace(/<p.*>/gi, "
");
    returnText=returnText.replace(/<a.*href="(.*?)".*>(.*?)</a>/gi, " $2 ($1)");

    //-- remove all inside SCRIPT and STYLE tags
    returnText=returnText.replace(/<script.*>[wW]{1,}(.*?)[wW]{1,}</script>/gi, "");
    returnText=returnText.replace(/<style.*>[wW]{1,}(.*?)[wW]{1,}</style>/gi, "");
    //-- remove all else
    returnText=returnText.replace(/<(?:.|s)*?>/g, "");

    //-- get rid of more than 2 multiple line breaks:
    returnText=returnText.replace(/(?:(?:
|
|
)s*){2,}/gim, "

");

    //-- get rid of more than 2 spaces:
    returnText = returnText.replace(/ +(?= )/g,'');

    //-- get rid of html-encoded characters:
    returnText=returnText.replace(/ /gi," ");
    returnText=returnText.replace(/&/gi,"&");
    returnText=returnText.replace(/"/gi,'"');
    returnText=returnText.replace(/</gi,'<');
    returnText=returnText.replace(/>/gi,'>');

    //-- return
    document.getElementById("output").value = returnText;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #replace #espacial #characteres #string
ADD COMMENT
Topic
Name
4+7 =