Today, I encountered the problem of converting Chinese text and English punctuation.
Find a solution record from the Internet
Html code
<Html>
<Head>
<Script language = "javascript">
Function ChineseToEnglish (txt)
{
Var ChineseInterpunction = ["", "," '","' ",", ". ",",",";",":","? ","! ","...... ","-","~ "," (",") "," "," "];
Var EnglishInterpunction = ["\"","\"","'","'",".",",",";",":","? ","! ","... ","-","~ "," (",") "," <","> "];
For (var j = 0; j <ChineseInterpunction. length; j ++)
{
// Alert ("txt. replace (" + ChineseInterpunction [j] + "," + EnglishInterpunction [j] + ")");
Var reg = new RegExp (ChineseInterpunction [j], "g ");
Txttxt = txt. replace (reg, EnglishInterpunction [j]);
}
Alert (txt );
Return txt;
}
// Method Description: Convert the full-width characters in the specified input box to half-width characters, and automatically change the full-width characters in the input box when the input box loses focus.
// Principles:
// 1. The fullwidth space is 12288, And the halfwidth space is 32.
// 2. The relationship between the half-width (33-126) of other characters and the full-width (65281-65374) is as follows: the difference is 65248.
Function FullToDBC (obj ){
Var Str = obj. value;
Var DBCStr = "";
Str = ChineseToEnglish (Str );
If (/. * [\ u4e00-\ u9fa5] +. * $/. test (Str )){
Alert ("contains Chinese characters! ");
}
For (var I = 0; I <Str. length; I ++ ){
Var c = Str. charCodeAt (I );
If (c = 12288 ){
DBCStr + = String. fromCharCode (32 );
Continue;
}
If (c> 65280 & c <65375 ){
DBCStr + = String. fromCharCode (c-65248 );
Continue;
}
DBCStr + = String. fromCharCode (c );
}
Obj. value = DBCStr;
}
</Script>
</Head>
<Body>
<Center>
<Textarea id = "testStr" rows = "20" cols = "120" onblur = "FullToDBC (this);"> </textarea>
</Center>
</Body>
</Html>
Author: "Seth Xu"