首页 > Web开发 > 详细

JavaScript replace() 方法几个实例

时间:2014-01-22 16:31:50      阅读:401      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
<!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=utf-8" />
<title>无标题文档</title>
<style  type="text/css">
p {
    font-weight:bold;
}
</style>
</head>

<body>

<p>使用 replace() 来转换姓名的格式 </p>
<script type="text/javascript">
name = "Doe, John";
document.write(name)
document.write("<br/>")
document.write(name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1"));
</script> 
<br />
<br />

<p>用 replace() 来转换引号 </p>
<script type="text/javascript">
name = "a", "b";
document.write(name)
document.write("<br/>")
document.write(name.replace(/"([^"]*)"/g, "‘$1‘"));
document.write("<br/>")
</script> 
<br />
<br />

<p>单词首字母大写 </p>
<script type="text/javascript">
name = aaa bbb ccc;
document.write(name)
uw=name.replace(/\b\w+\b/g, function(word){
    return word.substring(0,1).toUpperCase()+word.substring(1);}
    );
document.write("<br/>")
document.write (uw);
</script>

</body>
</html>
bubuko.com,布布扣

完全不知所以云。。。

JavaScript replace() 方法几个实例

原文:http://www.cnblogs.com/super-zhen/p/3529483.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!