页面代码 注释'Detail' 是TextArea(编辑框)的name
<img width="28px" height="28px" src="/Images/face/wx.jpg" id="wx" onmousemove="Exercise('wx','.gif')" onmouseout="StaticImg('wx','.jpg')" onClick="document.forms[0].Detail.value+='[/wx] ';document.forms[0].Detail.focus()"/>
后台代码
先了解下正则
(?<=\[\/)本表达式匹配 [/ 后面的字符串
(?=\])本表达式匹配 ] 前面的字符串
(?<=\[\/)[a-zA-Z]*(?=\]) 本表达式匹配 [/ 和]之间的a-zA-Z的所有字符。
后台方法
/// <summary>
/// 评论图片添加
/// </summary>
/// <param name="html">字符串</param>
/// <returns></returns>
public string PLContent(string html) {
MatchCollection temp;
Regex regex1 = new Regex(@"\[\/[a-zA-Z]*\]"); //正则表达式
Regex regex2 = new Regex(@"(?<=\[\/)[a-zA-Z]*(?=\])");//正则表达式
// html = regex1.Replace(html, "<img src=\"/Images/face/wx.jpg\" />");
temp = regex1.Matches(html);
for (int i = 0; i < temp.Count; i++)
{
html = html.Replace(temp[i].Value, "<img src=\"/Images/face/" + regex2.Matches(temp[i].Value)[0].Value + ".gif\" />");
}
return html;
}
浙公网安备 33010602011771号