<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 output:-
("<EM CLASS="Math-C">g</EM>", should be replaced with "⇔").
("<EM CLASS="Math-B">i</EM>", should be replaced with "~").
with following code i can replace all this tags easily,but i want this to be converted into javascript code, so that it should read the data from text area and then replace all such tags with another 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 help of above code i can replace the tags,but i need this java code to be converted into java Script, through which data pasted to text area,can be easily replaced by clicking on button. http://jsfiddle.net/yk6Tq/4/ (for reference)
Any one can please help me out!
what you can do is create an array of tags that 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 the 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]);
here's a working JSFiddle for the same.
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 the help of this , we can find and replace multiple lines as well as same line text. Thank you @Sushil.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.