JavaScript replace() 方法几个实例

<!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>

完全不知所以云。。。

posted @ 2014-01-22 11:37  fjenny_贞  阅读(244)  评论(0编辑  收藏  举报