Is it possible to find such tags from the html page pattern in the text area and replace the tags with other tags specified in the javascript code?

<HTML>

<HEAD>
</HEAD>

<BODY>
    <TABLE>
        <TR>
            <TD ROWSPAN="1" COLSPAN="1">
                <P CLASS="text">HR-CT</P>
            </TD>
            <TD ROWSPAN="1" COLSPAN="1">
                <P CLASS="Tab-Text-7">my
                    <EM CLASS="Math-B">i</EM> Stackoverflow
                    <EM 
                    CLASS="Math-B">i</EM>
                    <EM CLASS=
                    "Math-B">i</EM>
                    <EM CLASS="Math-B">g</EM> UIP-Muster
                    <EM CLASS="Math-B">m</EM>
                </P>
            </TD>
        </TR>
        <TR>
            <TD>
                <EM CLASS="hoch">C</EM>
                <EM CLASS="Math-C">d</EM>
            </TD>
        </TR>
    </TABLE>
</BODY>

</HTML>

      

Expected Result: -

("<EM CLASS="Math-C">g</EM>", should be replaced with "&#8660;").
("<EM CLASS="Math-B">i</EM>", should be replaced with "&#126;").

      

with the following code, I can easily replace all these tags, but I want this to be converted to javascript code so that it reads data from the text area and then replaces all such tags with other tags.

Java code:

FileInputStream fstream;
    FileOutputStream fout;
    DataInputStream in1;
    BufferedReader br;
    BufferedWriter bw=null;
    String htmlText=null;


    String line;
            fstream = new FileInputStream("D:\\Special symbol 
files\\test symbol.htm");
            String st=" ";
            fout = new FileOutputStream("D:\\Special symbol files\\test 
symbol new.html",true);
            in1 = new DataInputStream(fstream);
            br = new BufferedReader(new InputStreamReader(in1,"UTF-8"));
            reader = new FileInputStream("D:\\Special symbol files\\test 
symbol.htm");           
            bw=new BufferedWriter(new OutputStreamWriter(fout,"UTF-8"));
            while ((line=br.readLine()) != null) 
            {
                htmlText=htmlText+line;
            }
            //htmlText=extractText(reader);
            System.out.println("html content :"+htmlText);
            System.out.println("index:"+htmlText.indexOf("<EM  
CLASS=\"Math-C\">o</EM>"));



            htmlText=htmlText.replace("<EM CLASS=\"Math-B\">i</EM>", 
"&#126;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-B\">l</EM>", 
"<EM CLASS=\"Math-B\">l</EM>");
            htmlText=htmlText.replace("<EM CLASS=\"Math-B\">j</EM>", 
"<EM CLASS=\"Math-B\">j</EM>");
            htmlText=htmlText.replace("<EM CLASS=\"Math-B\">g</EM>", 

"<EM CLASS=\"Math-B\">g</EM>");
             /*pattern = Pattern.compile("<EM CLASS=\"Math-B\">\\s*m\\s*</EM>"); 
             m = pattern.matcher(htmlText);
                htmlText=m.replaceAll("&ge;");*/
            htmlText=regExp(htmlText,"Math-B","m","&ge;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-B\">+</EM>", "&#247;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-B\">[</EM>", "&le;");

            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">o</EM>", "&#8595;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">m</EM>", "&#8593;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">u</EM>", "&#8658;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">e</EM>", "&#8658;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">t</EM>", "&#8594;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">d</EM>", "&#8594;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">r</EM>", "&#8592;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">b</EM>", "&#8592;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">w</EM>", "&#8660;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">g</EM>", "&#8660;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">s</EM>", "&#8656;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">c</EM>", "&#8656;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">n</EM>", "&#8657;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">p</EM>", "&#8659;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">v</EM>", "&#8596;");
            htmlText=htmlText.replace("<EM CLASS=\"Math-C\">f</EM>", "&#8596;");

            htmlText=htmlText.replace(">", ">\n");
            bw.write(htmlText);
            bw.close();

      

with the above code I can replace the tags, but I need this java code to convert to java Script through which the data inserted into the text area can be easily replaced by clicking on the button. http://jsfiddle.net/yk6Tq/4/ (for reference)

Anyone can help me!

+3


source to share


2 answers


what you can do is create an array of tags you want to replace like this

var array = {
            '<EM CLASS="Math-C">d</EM>':'&#8660;', 
            '<EM CLASS="hoch">C</EM>':'<sup>C</sup>',
            '<EM CLASS="Math-B">g</EM>':'&#156;',
            '<EM CLASS="Math-B">i</EM>':'&#126;'
        }

      

now you can loop through the array and replace values ​​from your string.



var originalText = $('#txtArea').val();
var finalText = originalText;

for (var val in array)
   finalText = finalText.replace(new RegExp(val, "g"), array[val]);

      

there is a JSFiddle working here for the same.

+1


source


This is the solution for my question.

$(document).ready(function(){

   $('#findAndReplace').on('click', function(){

     var array = {
        '<EM CLASS="text">((\n|.*?))d</EM>':'&#8660;', 
        '<EM CLASS="data">((\n|.*?))c</EM>':'<sup>C</sup>',
        '<EM CLASS="data1">((\n|.*?))g</EM>':'&#156;',

    }

    var originalText = $('#txtArea').val();
    var finalText = originalText;

    for (var val in array)
        finalText = finalText.replace(new RegExp(val, "g"), array[val]);

    $('#txtArea').val(finalText);


  });

});

      



with this we can find and replace multiple lines as well as the same line text. Thanks @Sushil.

+1


source







All Articles