Replace Chinese Punctuation with English and fullwidth punctuation with halfwidth

Source: Internet
Author: User

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"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.