replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string), replaceText是替代查找到的字符串。为了帮助大家更好的理解,下面举个简单例子说明一下:
- <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>
- <P> </P>
复制代码 比我聪明的你,看完上面的例子之后,会发现第二个错别字"终古"并没有被替换成"中国",我们可以执行二次replace方法把第二个错别字"终古"也替换掉,程序经过改进之后如下:
- <script language="javascript">
- var stringObj="终古人民共和国,终古人民";
- //替换错别字"终古"为"中国"
- //并返回替换后的新字符
- //原字符串stringObj的值没有改变
- var newstr=stringObj.replace("终古","中国");
- newstr=newstr.replace("终古","中国");
- alert(newstr);
- </script>
复制代码 我们可以仔细的想一下,如果有N的N次方个错别字,是不是也要执行N的N次方replace方法来替换掉错别字呢??呵,不用怕,有了正则表达式之后不用一个错别字要执行一次replace方法。。程序经过改进之后的代码如下
- <script language="javascript">
- var reg=new RegExp("终古","g"); //创建正则RegExp对象
- var stringObj="终古人民共和国,终古人民";
- var newstr=stringObj.replace(reg,"中国");
- alert(newstr);
- </script>
复制代码
|