<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>
预期产出: -
("<EM CLASS="Math-C">g</EM>", should be replaced with "⇔").
("<EM CLASS="Math-B">i</EM>", should be replaced with "~").
使用以下代码我可以轻松替换所有这些标签,但我希望将其转换为javascript代码,以便它应该从文本区域读取数据,然后用其他标签替换所有这些标签。
Java代码:
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();
在上面的代码的帮助下我可以替换标签,但是我需要将这个java代码转换成java脚本,通过它可以通过单击按钮轻松替换粘贴到文本区域的数据。 http://jsfiddle.net/yk6Tq/4/(供参考)
任何人都可以帮助我!
答案 0 :(得分:1)
你可以做的是创建一个你想要替换的标签数组,如下所示
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>':'~'
}
现在您可以遍历数组并替换字符串中的值。
var originalText = $('#txtArea').val();
var finalText = originalText;
for (var val in array)
finalText = finalText.replace(new RegExp(val, "g"), array[val]);
这是一个有效的JSFiddle。
答案 1 :(得分:1)
这是我的问题的解决方案。
$(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);
});
});
借助于此,我们可以找到并替换多行以及相同的行文本。 谢谢@Sushil。