Find and replace text inside quotes, ignoring html tags

I have plain text with HTML tags and some text in quotation marks. I want to add span to text inside quotes. eg:

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

And I want to change this line 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 use this code for this:

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

but does it replace the HTML anchor tag quotes, as well as any solution? In short, I just want to add the spanquotes inside the quotes, ignoring the quotes of the HTML tags.

+4
source share
3 answers

JavaScript DOM- - , HTML , - , DOM . 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
});

.

+4

, . , .

, ur, , - span html (Ex: <a ... <span>) HTML , , .

. Java,

,

0

:

  • HTML
  • ,

1:

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

lookahead, , > <

2:

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

3:

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

0

All Articles