我正在尝试这样的事情:
var paragraphy= "Lorem ipsum dolar amet. Lorem ipsum dolar amet."
var uniqueid=Math.random().toString( 16 ).slice( 2, 10 );
paragraphy.replace(/dolar/gi,'<span id="'+uniqueid+'"'>$1</span>);
结果:
... <span id='ce79657a'>dolar</span>... ipsum <span id='ce79657a'>dolar</span> ...
但我希望得到这样的结果:
... <span id='ce79657a'>dolar</span>... ipsum <span id='ad45234f'>dolar</span> ...
我该怎么做?谢谢你的帮助。
答案 0 :(得分:0)
var paragraphy = "Lorem ipsum dolar amet. Lorem ipsum dolar amet."
paragraphy = paragraphy.replace(/dolar/gi, function(m) {
var uniqueid = Math.random().toString(16).slice(2, 10);
return '<span id="' + uniqueid + '">' + m + '</span>'
});
console.log(paragraphy);
&#13;