本文实例讲述了js使用正则表达式实现关键字替换加粗功能的方法。分享给大家供大家参考,具体如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>正则替换</title>
</head>
<body>
<script type="text/javascript">
var str = "使用正则表达式把关键字替换加粗,文字加粗";
var word = "加粗";
str = str.replace(new regexp("(" + word + ")","ig"), "<strong>" + word + "</strong>");
document.write(str);
</script>
</body>
</html>
运行效果截图如下:
希望本文所述对大家javascript程序设计有所帮助。
更多js使用正则表达式实现关键字替换加粗功能示例。