我有一個這樣的字符串:使用Javascript/jQuery的:替換單引號的文本與HTML span標籤
This is a 'item:value' and another 'item2:value:2'.
我想使用JavaScript/jQuery來得到以下結果:
This is a <span class="text">item:value</span> and another <span class="text">item2:value:2</span>.
我有一個這樣的字符串:使用Javascript/jQuery的:替換單引號的文本與HTML span標籤
This is a 'item:value' and another 'item2:value:2'.
我想使用JavaScript/jQuery來得到以下結果:
This is a <span class="text">item:value</span> and another <span class="text">item2:value:2</span>.
可以運行單引號之間的文本正則表達式replace()
:
var str = "This is a 'item:value' and another 'item2:value:2'."
var newString = str.replace(/'(.*?)'/g, function myFunction(x){
x = "<span class='text'>"+ x +"</span>"
return x;
})
謝謝,那爲我工作:) – cosmicstar23
使用concatination:
$("div").html('This is a <span class="text">' + item + ':' + value + '</span> and another <span class="text">' + item2 + ':' + value2 + '</span>');
試試這個:
<script>
var string = "This is a 'item:value' and another 'item2:value:2'.";
var res = string.split("'");
var output = res[0] + "<span class='text'>" + res[1] + "</span>" + res[2] + "<span class='text'>" + res[3] + "</span>" + res[4] ;
console.log(output);
</script>
希望這是你的意思。
你好,歡迎堆棧溢出!請閱讀[幫助文檔](http://stackoverflow.com/help/how-to-ask)以瞭解如何提出一個好問題。 –
我不太確定我的答案是你要做什麼的。如果不是,請說清楚一點。 – haltersweb