在标签前查找文本

时间:2018-12-05 06:12:21

标签: jquery regex replace find

我在下面的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>

我需要获取<xref>元素中所有<p>元素之前的文本。

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>

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

2 个答案:

答案 0 :(得分:1)

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

但是正如您所见,这是一个简单的非嵌套情况,因此您可以使用此正则表达式,

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

捕获单词(包括点),后跟xref标签并将其替换为

<u>\1</u>

Demo

答案 1 :(得分:0)

完成,在这里尝试

https://jsfiddle.net/0ohb85cr/

  $('xref').each(function(){
    var p=$(this).parents('p');
    var idx=p.html().indexOf('<xref>');
    var str=p.html().slice(0,idx);
    alert(str.replace(/(<([^>]+)>)/ig,''));
});