代碼如下: 
str = str.<br /> 
replace( /&(?!#?/w+;)/g , '&').<br /> 
replace( /undefinedundefined([^undefinedundefined]*)"/g , '“$1”' ).<br /> 
replace( /</g , '<' ).<br /> 
replace( />/g , '>' ).<br /> 
replace( /…/g , '…' ).<br /> 
replace( /“/g , '“' ).<br /> 
replace( /”/g , '”' ).<br /> 
replace( /‘/g , '‘' ).<br /> 
replace( /'/g , ''' ).<br /> 
replace( /—/g , '—' ).<br /> 
replace( /–/g , '–' ); 
 
上面這個還算短了,我看過一些論壇的JS代碼,在把Wind Code轉換成HTML時,那真是瘋子似的寫上二三十行。其實我們大可以把這些匹配模式與替換后的字符放到一個哈希中,然后一口氣替換掉。 
 代碼如下: 
var hash = { 
'<' : '<' , 
'>' : '>', 
'…' : '…', 
'“' : '“' , 
'”' : '”' , 
'‘' : '‘' , 
''' : ''' , 
'—' : '—', 
'–' : '–' 
}; 
str = str. 
replace( /&(?!#?/w+;)/g , '&' ). 
replace( /undefinedundefined([^undefinedundefined]*)"/g , '“$1”' ). 
replace( /[<>…“”‘'—–]/g , function ( $0 ) { 
return hash[ $0 ]; 
}); 
 
但這個缺陷也很明顯,如哈希的鍵必須是簡單的普通字符串,不能是復雜正則,這就是我們不得不分開的原因。replace在老一點的瀏覽器是不支持function的。為此,我們只好放棄上面最后那個replace方式,替換方統一為普通字符串。 
 代碼如下: 
String.prototype.multiReplace = function ( hash ) { 
var str = this, key; 
for ( key in hash ) { 
if ( Object.prototype.hasOwnProperty.call( hash, key ) ) { 
str = str.replace( new RegExp( key, 'g' ), hash[ key ] ); 
} 
} 
return str; 
}; 
 
Object.prototype.hasOwnProperty.call( hash, key )是用來過濾繼承自原型的方法與屬性的。這樣一來,使用就簡單了: 
 代碼如下: 
str = str.multiReplace({ 
'&(?!#?//w+;)' :'&', 
'undefinedundefined([^undefinedundefined]*)" : '“$1”', 
'<' : '<' , 
'>' : '>', 
'…' : '…', 
'“' : '“' , 
'”' : '”' , 
'‘' : '‘' , 
''' : ''' , 
'—' : '—', 
'–' : '–' 
});