I’ve some HTML structure and i want to search particular text inside that HTML structure. see below example
JavaScript
x
7
1
<h1>Try to span some text on this page</h1>
2
<div>
3
span is a <span>paragraph</span>.</div>
4
<p class="span ddd">This 123is a paragraph.1</p>
5
6
<div class="span ddd">This is some span in a div element.</div>
7
Now if I’m searching for “span” text then, “span” word should be wrapped with div tag. But only “span” word in text and not from class or other tags.
i want output like this (wrap using div or span)
JavaScript
1
2
1
<h1>Try to <span class="search">span</span> some text on this page</h1> <div> <span class="search">span</span> is a <span>paragraph</span>.</div> <p class="span ddd">This 123is a paragraph.1</p> <div class="span ddd">This is some <span class="search">span</span> in a div element.</div>
2
Advertisement
Answer
JavaScript
1
10
10
1
<div id="wrapper">
2
<h1>Try to span some text on this page</h1>
3
<div>
4
span is a <span>paragraph</span>.
5
</div>
6
<p class="span ddd">This 123is a paragraph.1</p>
7
8
<div class="span ddd">This is some span in a div element.</div>
9
</div>
10
I have modifed your html by enclosing with a wrapper for getting my selector. But you can use your own selectors.
JavaScript
1
5
1
$(document).ready(function(){
2
var str=$('#wrapper').html();
3
$('#wrapper').html(str.replace( new RegExp(" span ","g"),"<div>span</div>"));
4
});
5
This code will do what you need. Here is the jsffiddle for this.
If your problem is fixed then please don’t forget to mark this as solved. Thanks.