简体   繁体   English

查找并替换引号内的文本忽略html标记

[英]find and replace text inside quotes ignoring html tags

I have simple text with HTML tags and some text in quotation marks I want to add span on text inside quotes. 我有HTML标签的简单文本和引号中的一些文本我想在引号内的文本上添加span。 for example: 例如:

<p>A quick "brown" fox "jumps" over <a href="www.gamescottage.com">the</a> lazy dog.</p>

And I want is to change this line to something like this: 我想要将此行更改为以下内容:

<p>A quick "<span>brown</span>" fox "<span>jumps</span>" over <a href="www.gamescottage.com">the</a> lazy dog.</p>

and I am using this this code to do this : 我正在使用此代码执行此操作:

<script>
    $('document').ready(function (){
        var text = $('p').html();
        text = text.replace(/"(.*?)"/g, '"<span class="quote">$1</span>"');
        $('p').html(text);
     });
</script>

but it replace quotes of HTML anchor tag as well any solution? 但它取代了HTML锚标记的引用以及任何解决方案? In short I just want to add span inside quotes ignoring quotes of HTML tags. 简而言之,我只想在引号中添加span而忽略HTML标记的引号。

JavaScript already has a built on DOM parser for you - instead of trying to parse HTML with a regular expression which is inherently difficult and borderline impossible - you can use the built in abilities the DOM brings you. JavaScript已经为你构建了一个内置的DOM解析器 - 而不是试图用正则表达式来解析HTML本身很困难且边缘不可能 - 你可以使用DOM为你带来的内置功能。 In your case I'll demonstrate it using jQuery but a non-jQuery solution is equally simple: 在你的情况下,我将使用jQuery演示它,但非jQuery解决方案同样简单:

$("p"). // all p tags
contents(). // select the actual contents of the tags 
filter(function(i,el){   return el.nodeType === 3; }). // only the text nodes
each(function(i, el){ 
    var $el = $(el); // take the text node as a jQuery element
    var replaced = $el.text().replace(/"(.*?)"/g,'<span>"$1"</span>') // wrap
    $el.replaceWith(replaced); // and replace
});

Here's a fiddle . 这是一个小提琴

From your code I assume you are good at regex and scripts . 从你的代码我假设你擅长正则表达式和脚本。 Here is a algorithm using which you can write your code. 这是一个算法,您可以使用它编写代码。

after replacing text with spans as you do in ur current code, take the resultant string and check for if any span tag present inside a open html tag (Ex: <a ... <span> ) and replace that whole HTML entity from the original String. 在您使用当前代码替换文本后,取出结果字符串并检查打开的html标记内是否存在任何span标记(例如: <a ... <span> )并将整个HTML实体替换为原始字符串。 By this you can achieve what you need. 通过这个你可以实现你所需要的。

Note: I have done the same in Java once I dont have the snippet with me now 注意:我已经在Java中完成了相同的操作,因为我现在没有使用该代码片段

Hope this helps you 希望这对你有所帮助

You could do this in three separate steps: 您可以通过三个单独的步骤执行此操作:

  1. temporarily replace quotes in HTML tags with a marker 使用标记临时替换HTML标记中的引号
  2. add spans to any remaining quotes 为任何剩余的报价添加跨度
  3. restore quotes where any markers were put 恢复任何标记放置的引号

Step 1: 步骤1:

text = text.replace(/([^>"]*)"(?=[^<]*>)/g, '$1#Q#');

This uses a lookahead to see whether the quote is followed by a > before the next < 这使用先行看是否报价之后是>之前的下一个<

Step 2: 第2步:

text = text.replace(/"(.*?)"/g, '<span class="quote">$1</span>');

Step 3: 第3步:

text = text.replace(/#Q#/g, '"');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM