使用jQuery我有这个例子: 简单的json:
var data = {"words": [
{"word":"angry", "wordtext":"<p>I´m very very <strong>angry</strong>.</p>"},
{"word":"tired", "wordtext":"<p>I´m very very <strong>tired</strong>.</p>"}
]};
某段落:
<p>bla bla bla bla **angry** bla bla bla **tired** bla bla bla ...</p>
我希望在段落内部(如果存在)替换单词(例如,#34;愤怒&#34;):
<a href="some">word</a>
答案 0 :(得分:0)
使用此正则表达式:
\*\*(.+?)\*\*
并将其替换为:
<a href="some">$1</a>
答案 1 :(得分:0)
您可以获取文字并替换为html
$('p').each(function() {
var txt = $(this).text().replace(/\*\*(.+?)\*\*/g, '<a href="$1">$1</a>');
$(this).html(txt)
})
答案 2 :(得分:0)
您的Html和demo
<a href="some" id="yourid">word</a>
你的js
$(function(){
var i=0;
$.each(data.words[i],function(index,item){
// alert(item)
i++;
if(item=='angry'){
$('#yourid').html(item);
}
});
});