d = { "id" : "abcde",
"key1" : "blah",
"key2" : "blah blah",
"nestedlist" : [
{ "id" : "qwerty",
"nestednestedlist" : [
{ "id" : "xyz", "keyA" : "blah blah blah" },
{ "id" : "fghi", "keyZ" : "blah blah blah" }],
"anothernestednestedlist" : [
{ "id" : "asdf", "keyQ" : "blah blah" },
{ "id" : "yuiop", "keyW" : "blah" }] } ] }
def findkeys(node, kv):
if isinstance(node, list):
for i in node:
for x in findkeys(i, kv):
yield x
elif isinstance(node, dict):
if kv in node:
yield node[kv]
for j in node.values():
for x in findkeys(j, kv):
yield x
print list(findkeys(d, 'id'))
здесь вы идете
$(".demo-text").html($(".demo-text").html().replace(/Mike/g, "<span class='index-pg'>Mike</span>"));
Вы можете просто добавить следующий jquery.
jQuery(document).ready(function(){
var str = jQuery(".demo-text").text();
var res = replaceAll(str,'Mike','');
jQuery(".demo-text").text(res);
});
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
Вот рабочая демонстрация: https://jsfiddle.net/manektech/37p5y2jk/5/
Вы можете зациклить внутри span
текст и обернуть keyword
с span
с желанием class
.
var keyWord = 'Mike';
var replaceMent = "<span class='index-pg'>" + keyWord + "</span>";
$(".demo-text").each(function() {
var txt = $(this).text();
var regex = new RegExp("\\b" + keyWord + "\\b", "gi");
txt = txt.replace(regex, replaceMent);
$(this).html(txt);
});
.demo-text {}
.index-pg {
color: red;
background: black;
height: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class="demo-text">My name is Mike and there are seven more people with name Mike in this building </span>