以下是我按照《javascript语言精粹》一书中的代码写的demo,我想做的事情是历遍文本,把文本中出现的所有单词以只出现一次的形式打印出来(我不知道书中说的“doubled_words”是不是这个意思,不管是不是了,我现在想现实我说的这个结果 ),但是好像没达到我的要求,请教了。
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#test1,#test2{width:500px;height:200px;border:1px solid #00f;margin-bottom:20px;}
</style>
<script type="text/javascript">
window.onload = function () {
var test1 = document.getElementById('test1'),
test2 = document.getElementById('test2'),
textSource = test1.innerHTML,
textEscape;
var textRegExp = /([A-Za-z\u00C0-\u1FFF\u2800-\uFFFD'\-]+)\s+\1/g;//定义一个重复的单词
textEscape = textSource.replace(textRegExp,"$1");
test2.innerHTML = textEscape;
}
</script>
</head>
<body>
<div id="test1">activity Sizzle It! is is the expert in producing sizzle reels that capture your message and captivate your audience — all with creativity and style. expert sizzle reels that capture</div>
<div id="test2"></div>
</body>
</html>
注:我要的效果是:
<div id="test2">activity Sizzle It! is the expert in producing reels that capture your message and captivate audience — all with creativity style.</div>