有没有可能从粘贴在文本区域的html页面中找到这样的标签,并用javascript代码中提到的另一个标签替换它的标签

Is it possible to find such tags from a html page paste on text area and replace its tags with another tags mentioned in javascript code?

本文关键字:标签 并用 javascript 代码 替换 另一个 有可能 区域 文本 html      更新时间:2023-09-26
<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 "&#8660;").
("<EM CLASS="Math-B">i</EM>", should be replaced with "&#126;").

使用下面的代码,我可以很容易地替换所有这些标签,但我希望将其转换为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>", 
"&#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();

在上面代码的帮助下,我可以替换标签,但我需要将这段java代码转换为java脚本,通过它可以通过点击按钮轻松替换粘贴到文本区域的数据。http://jsfiddle.net/yk6Tq/4/(供参考(

任何人都可以帮我!

您所能做的就是创建一个要替换的标签数组,就像这个一样

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;'
        }

现在,您可以循环遍历数组并替换字符串中的值。

var originalText = $('#txtArea').val();
var finalText = originalText;
for (var val in array)
   finalText = finalText.replace(new RegExp(val, "g"), array[val]);

这里有一个同样的工作JSFiddle。

这是我的问题的解决方案。

$(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);

  });
});

借助于此,我们可以查找和替换多行以及同一行文本。谢谢你@Sushil。