我应该使用什么正则表达式来获取css类的内容并存储它们?

What regex shall I use to fetch the content of css classes and store them?

本文关键字:存储 什么 正则表达式 css 获取 我应该      更新时间:2023-09-26

您好,我已经创建了一个文本框,用于从PDF中复制内容并接受富文本格式的内容。

<html>
<head>
 <link rel="stylesheet" type="text/css" href="Theme.css"> 
</head>
<body>
<div>
    <textarea id="ta" onpaste="functionItalic(event)" class="foostyle2"></textarea>
</div>
<div>
    <span style="font-weight: bolder; font-size: 20px;">
        <span id="1">Karan's</span>
     </span>
    <span style="font-weight: bolder; font-size: 24px; font-style: italic;">test</span>
</div>
<script>
function functionItalic(pasteEvent)
{
var textareacont = (pasteEvent.originalEvent || pasteEvent).clipboardData.getData("text/html");
console.log(textareacont);
}
</script>
</body>
</html>

当我在控制台上打印内容时(内容是对于烟雾保护的装配座位,常见的),我发现PDF中的内容包含像这样的css类和html标签

:此代码是在执行console.log(textareacont);时获得的对于防烟总成座位,一般采用

CSS Tweaking.html (line 19)
<html>
<head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><style>
<!--
br
{
mso-data-placement:same-cell;
}
table
{
mso-displayed-decimal-separator:"'.";
mso-displayed-thousand-separator:"', ";
}
tr
{
mso-height-source:auto;
mso-ruby-visibility:none;
}
td
{
border:.5pt solid windowtext;
}
.NormalTable{cellspacing:0;cellpadding:10;border-collapse:collapse;mso-table-layout-alt:fixed;border:none; mso-border-alt:solid windowtext .75pt;mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-border-insideh:.75pt solid windowtext;mso-border-insidev:.75pt solid windowtext}
.fontstyle0
{
    font-family:Times-Roman;
    font-size:10pt;
    font-style:normal;
    font-weight:normal;
    color:rgb(0,0,0);
}
.fontstyle1
{
    font-size:12pt;
    font-style:normal;
    font-weight:normal;
    color:rgb(0,0,0);
}
.fontstyle2
{
    font-family:Times-Italic;
    font-size:10pt;
    font-style:italic;
    font-weight:normal;
    color:rgb(0,0,0);
}
-->
</style></head><body>
<!--StartFragment-->
<span class="fontstyle0">For </span><span class="fontstyle2">smoke-protected assembly seating</span><span class="fontstyle0">, the </span><span class="fontstyle2">common</span> 
<br style=" font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">
<!--EndFragment-->
</body>
</html>

我想要的是,我想要获得css类的属性名称。fontstyle1,。fontstyle2,。字符串格式的fontstyle3,有人说这可以通过RegEx谁能告诉我什么将是RegEx,我需要使用以便在字符串中存储。fontstyle类。我试过几个,但都不起作用。换行符、回车符和制表符换行符是字符串的一部分,只出现在类中。

如果有人知道另一种方法来存储。fontstyle类的内容在字符串。我不太懂正则表达式

你可以使用这个正则表达式

/'.fontstyle'd+'s*'{['w's-:;,()]*'}/g

我不确定我是否给了太多的帮助,但既然它已经编码了…

    // The regular expression
    var regularExp = /'.fontstyle'd+'s*'{['w's-:;,()]*'}/g;
    var match;
    // .fontstyle will be stored in fontstyle_list[]
    var fontstyle_list = [];
    // Finds all match
    while (match = regularExp.exec(/*The css file (converted to string) should go here*/)) {
        // Adds every match into fontstyle_list
        fontstyle_list.push(match[0]);
    }
    // Iterate through every element in fontstyle_list
    for (var i in fontstyle_list){
        // prints out each .fontstyle{}
        document.write(fontstyle_list[i] + "<br />"); // document.write() is unsafe and should only be used for testing
        // add your codes here
    }

2。另外

在使用子字符串检索每个以'开头的元素之前,你可以将css解析成一个对象。fontstyle '

  1. 将css解析为对象。参考https://stackoverflow.com/a/14865690/6943913

  2. 遍历步骤1中对象的所有元素
  3. 对于步骤2中的每个元素,执行<eachElement>.substring(0, 10) === ".fontstyle"
  4. 对于第3步中返回true的每个操作,将元素复制到另一个列表

*免责声明:上面的步骤只是为了说明程序逻辑,可能需要做一些调整以适应实际场景