以下是我按照《javascript語(yǔ)言精粹》一書(shū)中的代碼寫(xiě)的demo,我想做的事情是歷遍文本,把文本中出現(xiàn)的所有單詞以只出現(xiàn)一次的形式打印出來(lái)(我不知道書(shū)中說(shuō)的“doubled_words”是不是這個(gè)意思,不管是不是了,我現(xiàn)在想現(xiàn)實(shí)我說(shuō)的這個(gè)結(jié)果 ),但是好像沒(méi)達(dá)到我的要求,請(qǐng)教了。<!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;//定義一個(gè)重復(fù)的單詞
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>
我想做的事情是歷遍文本,把文本中出現(xiàn)的所有單詞以只出現(xiàn)一次的形式打印出來(lái),但好像沒(méi)達(dá)到效果
慕萊塢森
2023-04-08 14:10:02