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 "⇔").
("<EM CLASS="Math-B">i</EM>", should be replaced with "~").
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>",
"~");
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("≥");*/
htmlText=regExp(htmlText,"Math-B","m","≥");
htmlText=htmlText.replace("<EM CLASS=\"Math-B\">+</EM>", "÷");
htmlText=htmlText.replace("<EM CLASS=\"Math-B\">[</EM>", "≤");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">o</EM>", "↓");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">m</EM>", "↑");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">u</EM>", "⇒");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">e</EM>", "⇒");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">t</EM>", "→");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">d</EM>", "→");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">r</EM>", "←");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">b</EM>", "←");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">w</EM>", "⇔");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">g</EM>", "⇔");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">s</EM>", "⇐");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">c</EM>", "⇐");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">n</EM>", "⇑");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">p</EM>", "⇓");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">v</EM>", "↔");
htmlText=htmlText.replace("<EM CLASS=\"Math-C\">f</EM>", "↔");
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!
source to share
what you can do is create an array of tags you want to replace like this
var array = {
'<EM CLASS="Math-C">d</EM>':'⇔',
'<EM CLASS="hoch">C</EM>':'<sup>C</sup>',
'<EM CLASS="Math-B">g</EM>':'œ',
'<EM CLASS="Math-B">i</EM>':'~'
}
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]);
source to share
This is the solution for my question.
$(document).ready(function(){
$('#findAndReplace').on('click', function(){
var array = {
'<EM CLASS="text">((\n|.*?))d</EM>':'⇔',
'<EM CLASS="data">((\n|.*?))c</EM>':'<sup>C</sup>',
'<EM CLASS="data1">((\n|.*?))g</EM>':'œ',
}
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.
source to share