【问题标题】:Find Text before a Tag [closed]在标签前查找文本 [关闭]
【发布时间】:2019-05-06 15:53:27
【问题描述】:

我在下面的 XML 文件链接中有一个文本

<p>The artificial and industrial uses <xref>1989</xref> of microorganisms for material production have a long history of more than a thousand years. Recently, genetic operations have been widely applied to improve production. Two generally considered <xref>approaches, 2017</xref> introduce enzymes that have higher activities from other organisms or species and introduce enzymes to realize metabolic pathways that do not naturally occur in the microorganisms. The former method is popular because its operation is simpler and improvements are more predictable than <xref>(2001)</xref> with the latter method. <xref>2013</xref> Conventional gene modifications using ultraviolet or other radiation types are easy to achieve and have been widely applied in many industries. Nevertheless, the efficiency of such improvements is quite low because gene modifications occur accidentally and uncontrollably, and progress is made serendipitously. Therefore, gene introduction is currently used along with conventional methods.</p>

我需要在 &lt;p&gt; 元素中的所有 &lt;xref&gt; 元素之前获取文本。

var $element = $xml.find("p").addBack("p");
$element.each(function()
{
    //code here
});

输出喜欢

<p>The artificial and industrial <u>uses <xref>1989</xref></u> of microorganisms for material production have a long history of more than a thousand years. Recently, genetic operations have been widely applied to improve production. Two generally <u>considered <xref>approaches, 2017</xref></u> introduce enzymes that have higher activities from other organisms or species and introduce enzymes to realize metabolic pathways that do not naturally occur in the microorganisms. The former method is popular because its operation is simpler and improvements are more predictable <u>than <xref>(2001)</xref></u> with the latter <u>method. <xref>2013</xref></u> Conventional gene modifications using ultraviolet or other radiation types are easy to achieve and have been widely applied in many industries. Nevertheless, the efficiency of such improvements is quite low because gene modifications occur accidentally and uncontrollably, and progress is made serendipitously. Therefore, gene introduction is currently used along with conventional methods.</p>

我浏览了很多关于在元素之前查找文本但我没有得到解决方案。请提前提出解决方案谢谢

【问题讨论】:

  • 您应该避免使用正则表达式解析 html。

标签: jquery regex replace find


【解决方案1】:

您不应使用正则表达式解析 html,因为 html 可能具有导致意外行为的嵌套结构。

但正如我在您的案例中看到的,它是简单的非嵌套案例,因此您可以使用此正则表达式,

([\w.]+\s+(?:<xref>.*?<\/xref>))

捕捉一个单词(包括点)后跟xref标签并将其替换为

<u>\1</u>

Demo

【讨论】:

  • @Swashi:你能编辑你的帖子并添加一些你想要匹配的样本和一些你不想匹配的样本吗?
  • 如果您使用Skype,您可以搜索并添加我的id pkr555。我们可以在那里交谈
  • 是的,已经回复你了
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-16
  • 2016-11-09
  • 2019-11-03
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多