我有一个这样的字符串:
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>.
答案 0 :(得分:0)
使用concatination:
$("div").html('This is a <span class="text">' + item + ':' + value + '</span> and another <span class="text">' + item2 + ':' + value2 + '</span>');
答案 1 :(得分:0)
试试这个:
<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>
希望这就是你的意思。
答案 2 :(得分:0)
您可以在单引号之间的文本上运行正则表达式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;
})