开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1911|回复: 0
收起左侧

[JS相关教程] 上课了:JavaScript replace new RegExp使用

[复制链接]
发表于 2017-6-12 09:28:00 | 显示全部楼层 |阅读模式   北京市北京市
replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),
replaceText是替代查找到的字符串。为了帮助大家更好的理解,下面举个简单例子说明一下:

  1. <SPAN style="WHITE-SPACE: normal; WORD-SPACING: 0px; TEXT-TRANSFORM: none; FLOAT: none; COLOR: rgb(0,0,0); TEXT-ALIGN: center; FONT: 14px/25px tahoma, arial, 宋体; WIDOWS: 1; DISPLAY: inline !important; LETTER-SPACING: normal; BACKGROUND-COLOR: rgb(249,249,249); TEXT-INDENT: 28px; -webkit-text-stroke-width: 0px"></SPAN><DIV class="line number1 index0 alt2" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px"><script language=</CODE><CODE class="js string" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: blue !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">"javascript"</CODE><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">></CODE></DIV><DIV class="line number2 index1 alt1" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js keyword" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: bold !important; COLOR: rgb(0,102,153) !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">var</CODE> <CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">stringObj=</CODE><CODE class="js string" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: blue !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">"终古人民共和国,终古人民"</CODE><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">;</CODE></DIV><DIV class="line number3 index2 alt2" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js comments" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: rgb(0,130,0) !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">//替换错别字"终古"为"中国"</CODE></DIV><DIV class="line number4 index3 alt1" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js comments" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: rgb(0,130,0) !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">//并返回替换后的新字符</CODE></DIV><DIV class="line number5 index4 alt2" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js comments" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: rgb(0,130,0) !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">//原字符串stringObj的值没有改变</CODE></DIV><DIV class="line number6 index5 alt1" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js keyword" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: bold !important; COLOR: rgb(0,102,153) !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">var</CODE> <CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">newstr=stringObj.replace(</CODE><CODE class="js string" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: blue !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">"终古"</CODE><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">,</CODE><CODE class="js string" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: blue !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">"中国"</CODE><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">);</CODE></DIV><DIV class="line number7 index6 alt2" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px">alert(newstr);</CODE></DIV><DIV class="line number8 index7 alt1" style="LIST-STYLE-TYPE: none; BOX-SIZING: content-box !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: white; WHITE-SPACE: pre !important; WORD-SPACING: 0px; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; TEXT-TRANSFORM: none; FLOAT: none !important; COLOR: rgb(0,0,0); OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; TEXT-ALIGN: left; PADDING-TOP: 0px !important; FONT: 14px/15px Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; OUTLINE-STYLE: none !important; PADDING-LEFT: 1em !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; WIDOWS: 1; MARGIN: 0px; LETTER-SPACING: normal; OUTLINE-COLOR: invert !important; TOP: auto !important; PADDING-RIGHT: 1em !important; BOTTOM: auto !important; TEXT-INDENT: 0px; border-radius: 0px; -webkit-text-stroke-width: 0px"><CODE class="js plain" style="BOX-SIZING: content-box !important; FONT-SIZE: 1em !important; OVERFLOW: visible !important; BORDER-TOP: 0px; HEIGHT: auto !important; FONT-FAMILY: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; BORDER-RIGHT: 0px; WIDTH: auto !important; VERTICAL-ALIGN: baseline !important; BACKGROUND: none transparent scroll repeat 0% 0%; RIGHT: auto !important; BORDER-BOTTOM: 0px; POSITION: static !important; FLOAT: none !important; FONT-WEIGHT: normal !important; COLOR: black !important; OUTLINE-WIDTH: 0px !important; PADDING-BOTTOM: 0px !important; FONT-STYLE: normal !important; TEXT-ALIGN: left !important; PADDING-TOP: 0px !important; OUTLINE-STYLE: none !important; PADDING-LEFT: 0px !important; LEFT: auto !important; MIN-HEIGHT: auto !important; BORDER-LEFT: 0px; MARGIN: 0px; OUTLINE-COLOR: invert !important; LINE-HEIGHT: 1.1em !important; TOP: auto !important; PADDING-RIGHT: 0px !important; BOTTOM: auto !important; border-radius: 0px"></script></CODE></DIV>
  2. <P> </P>
复制代码
比我聪明的你,看完上面的例子之后,会发现第二个错别字"终古"并没有被替换成"中国",我们可以执行二次replace方法把第二个错别字"终古"也替换掉,程序经过改进之后如下:

  1. <script language="javascript">
  2. var stringObj="终古人民共和国,终古人民";

  3. //替换错别字"终古"为"中国"
  4. //并返回替换后的新字符
  5. //原字符串stringObj的值没有改变
  6. var newstr=stringObj.replace("终古","中国");
  7. newstr=newstr.replace("终古","中国");
  8. alert(newstr);
  9. </script>
复制代码
我们可以仔细的想一下,如果有N的N次方个错别字,是不是也要执行N的N次方replace方法来替换掉错别字呢??呵,不用怕,有了正则表达式之后不用一个错别字要执行一次replace方法。。程序经过改进之后的代码如下

  1. <script language="javascript">
  2. var reg=new RegExp("终古","g"); //创建正则RegExp对象
  3. var stringObj="终古人民共和国,终古人民";
  4. var newstr=stringObj.replace(reg,"中国");
  5. alert(newstr);
  6. </script>
复制代码

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表