Posted By

szsk on 08/03/06


Tagged

javascript String japanese


Versions (?)

string_h2z


 / Published in: JavaScript
 

URL: http://snipplr.com/users/szsk/

全角文字を対応する半角文字に

  1. Array.prototype.clone = function( ) {
  2. return Array.apply( null, this );
  3. }
  4. String.prototype.h2z = function( ignorechars ) {
  5. //無視文字列が渡されているなら配列を複製
  6. var tb = ( ignorechars ) ? h2z_tb.clone( ) : h2z_tb;
  7.  
  8. //無視する文字を消していく
  9. if( ignorechars ) {
  10. ignorechars = ignorechars
  11. .replace( /([カキクケコサシスセソタチツテトハヒフヘホ])゙/g, function( $0 ) {
  12. daku[ $0.charCodeAt( 0 ) ] = "";
  13. return "";
  14. } )
  15. .replace( /([ハヒフヘホ])゙/g, function( $0 ) {
  16. handaku[ $0.charCodeAt( 0 ) ] = "";
  17. return "";
  18. } )
  19. ;
  20. for( var i = 0, len = ignorechars.length; i < len; i++ )
  21. tb[ ignorechars.charCodeAt( i )] = "";
  22. }
  23.  
  24. return this
  25. .replace( /[\uFF76-\uFF84\uFF8A-\uFF8E]゙/g
  26. , function( $0 ) {
  27. return daku[ $0.charCodeAt( 0 ) ] || $0; } )
  28. .replace( /[\uFF8A-\uFF8E]゚/g
  29. , function( $0 ) {
  30. return handaku[ $0.charCodeAt( 0 ) ] || $0; } )
  31. .replace( /[\u0020-\u007E\uFF61-\uFF9F]/g
  32. , function( $0 ) {
  33. return tb[ $0.charCodeAt( 0 ) ] || $0; } )
  34. ;
  35. }
  36.  
  37. var h2z_tb = new Array;
  38.  
  39. var daku = new Array;
  40. daku["カ".charCodeAt( 0 )] = "ガ";
  41. daku["キ".charCodeAt( 0 )] = "ギ";
  42. daku["ク".charCodeAt( 0 )] = "グ";
  43. daku["ケ".charCodeAt( 0 )] = "ゲ";
  44. daku["コ".charCodeAt( 0 )] = "ゴ";
  45. daku["サ".charCodeAt( 0 )] = "ザ";
  46. daku["シ".charCodeAt( 0 )] = "ジ";
  47. daku["ス".charCodeAt( 0 )] = "ズ";
  48. daku["セ".charCodeAt( 0 )] = "ゼ";
  49. daku["ソ".charCodeAt( 0 )] = "ゾ";
  50. daku["タ".charCodeAt( 0 )] = "ダ";
  51. daku["チ".charCodeAt( 0 )] = "ヂ";
  52. daku["ツ".charCodeAt( 0 )] = "ヅ";
  53. daku["テ".charCodeAt( 0 )] = "デ";
  54. daku["ト".charCodeAt( 0 )] = "ド";
  55. daku["ハ".charCodeAt( 0 )] = "バ";
  56. daku["ヒ".charCodeAt( 0 )] = "ビ";
  57. daku["フ".charCodeAt( 0 )] = "ブ";
  58. daku["ヘ".charCodeAt( 0 )] = "ベ";
  59. daku["ホ".charCodeAt( 0 )] = "ボ";
  60.  
  61. var handaku = new Array;
  62. handaku["ハ".charCodeAt( 0 )] = "パ";
  63. handaku["ヒ".charCodeAt( 0 )] = "ピ";
  64. handaku["フ".charCodeAt( 0 )] = "プ";
  65. handaku["ヘ".charCodeAt( 0 )] = "ペ";
  66. handaku["ホ".charCodeAt( 0 )] = "ポ";
  67.  
  68. //半角英数記号→全角英数記号
  69. //!(0x21) - ~(007E)
  70. for( i = "!".charCodeAt( 0 ), len = "~".charCodeAt( 0 ); i <= len; i++ )
  71. h2z_tb[i] = String.fromCharCode( i + 0xFEE0 );
  72. h2z_tb[" ".charCodeAt( 0 )] = " ";
  73. h2z_tb["\\".charCodeAt( 0 )] = "¥";
  74.  
  75. //半角カナ→全角カナ
  76. var z = '。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゛゜';
  77. for( i = "。".charCodeAt( 0 ), len = "゚".charCodeAt( 0 ); i <= len; i++ )
  78. h2z_tb[i] = z.charAt( i - "。".charCodeAt( 0 ) );
  79.  
  80. //--------------------------//
  81.  
  82. alert( "アイウエオガギグゲゴパピプペポ。「」、・".h2z );
  83. //アイウエオガギグゲゴパピプペポ。「」、・

Report this snippet  

You need to login to post a comment.