登录  /  注册
在《javascript语言精粹》一书中,js正则表达式剔除重复的单词出错,什么原因?
天蓬老师
天蓬老师 2017-04-10 12:41:49
[JavaScript讨论组]

以下是我按照《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>
	<p 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</p>
	<p id="test2"></p>
</body>
</html>

注:我要的效果是:

<p id="test2">activity Sizzle It! is the expert in producing reels that capture your message and captivate audience — all with creativity style.</p>
天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号