日期:2014-05-16  浏览次数:20523 次

js 替换字符串指定一段字符的内容
var string=" <strong>张三:<span id=1>123</span></strong> <strong>李四:<span id=2>456</span></strong> <strong>小明:<span id=3>789</span></strong> ";  

我想把“<strong>张三:<span id=1>123</span></strong>” 里的“123”字符替换成“xxx”(但字符“123”是随机的,不能确定)
要怎样才能实现呢?感激各位!

------解决方案--------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
 </head>

 <body>
<script type="text/javascript">
<!--
var string=" <strong>张三:<span id=1>123</span></strong> <strong>李四:<span id=2>456</span></strong> <strong>小明:<span id=3>789</span></strong> ";

var str2 = string.replace(/>\d+</g,">xxx<");
alert(str2);
//-->
</script>
 </body>
</html>


看是不是这样的。