js正则截取替换特定字符之间的字符串

时间:2022-03-30 14:53:56

	var html="ssssss @用户名 和话题 #话题标题# 用不同颜色高@假的亮 @用户名2 和sss#此话题不对呀#ss";
var html1="ssssss @用户名 和话题 #话题标题# 用不同颜色高@假的亮 @用户名2 和sss#此话题不对呀#ss";
var reg1=/( @[^ ]* )/g;
var reg2=/( #[^#]*# )/g;
var reg3=/(#[^#]*#)/g;
document.write(html);
document.write("<br/>");
html=html.replace(reg1,"<span style='color:red'>$1</span>");
html=html.replace(reg2,"<span style='color:green;'>$1</span>");
document.write(html);
document.write("<br/>");
html1 = html1.replace(reg3,"<span style='color:blue;'>$1</span>");
document.write(html1);
document.write("<br/>");
	var a = "20141021123212";	var reg4 = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;	a = a.replace(reg4,"$1-$2-$3 $4:$5:$6");	document.write(a);	document.write("<br/>");
	var c ="好啊 {话题标题} 用不同颜色sss{此话题好}ss";	var reg5=/({[^{]*})/g;	c = c.replace(reg5,"<span style='color:red'>$1</span>");	document.write(c);	document.write("<br/>");