I am trying to create a script to replace all my double quote texts to be insert inside brackets.
Eg: Input : This "is" a "text". Output : This [is] a [text].
I have tried to fetch all the text elements and using regex replace and print the bracekts.
But it is not working as expected for nested elements.
This is my code sample
HTML
<p>this "is" a "text"
<small>this is "small" text</small>
</p>
<h1>"this" is "another"</h1>
Javascript
$(document).ready(function(){
$('p, h1, h2, h3, h4, h5, div, span, small, b, strong, li').each(function(){
var re = /([^"]*"[^"]*)"/gm;
var str = $(this).text();
var subst = '$1»';
var result = str.replace(re, subst);
var resultOdd = result.replace(/"/g, '«');
$(this).text(resultOdd);
});
})
This is the fiddle for the same.
Can anyone please help me?