ファイル名として不正な文字を任意の文字に置換
とりあえずWindowsの場合ということで…
String src = "abc_ABC_あいう_亜伊卯_アイウ_123_123_\\_/_:_*_?_\"_<_>_.txt"; // [/, *, ?, ", <, >, \] にマッチ Pattern illegalFileNamePattern = Pattern.compile("[(\\|/|:|\\*|?|\"|<|>|\\\\|)]"); String fileName = illegalFileNamePattern.matcher(src).replaceAll("-"); System.out.println("src :"+src); System.out.println("fileName :"+fileName);
実行結果
src :abc_ABC_あいう_亜伊卯_アイウ_123_123_\_/_:_*_?_"_<_>_.txt fileName :abc_ABC_あいう_亜伊卯_アイウ_123_123_-_-_-_-_-_-_-_-_.txt
ファイルパスをOKとする場合
// [\]をスルー Pattern illegalFileNamePattern = Pattern.compile("[(\\|/|:|\\*|?|\"|<|>|)]");
ASCII文字以外を任意の文字に置換
Javaの正規表現で POSIX 文字クラスってのがあるんですね
便利!^^
String src = "abc_ABC_あいう_亜伊卯_アイウ_123_123" // \p{ASCII} でASCII文字とマッチ、[^]で囲んでnot Pattern pattern = Pattern.compile("[^\\p{ASCII}]"); String dst = pattern .matcher(src).replaceAll("*"); System.out.println("src :"+dst); System.out.println("dst :"+dst);
実行結果
src :abc_ABC_あいう_亜伊卯_アイウ_123_123 dst :abc_***_***_***_***_123_***
参照
RFC3629でUTF-8のバイト範囲が変更されている
最新のUTF-8では5〜6バイトの表現がすべて不正になったんですね
知らなかった…(ぉぃ
最新と言っても RFC3629 って2003年?ずいぶん前だな
他にもコマゴマと範囲が変わったみたい
RFC2279(旧)
1バイト | 2バイト | 3バイト | 4バイト | 5バイト | 6バイト | |
---|---|---|---|---|---|---|
UTF8-1 | 0x00 〜 0x7F | - | - | - | - | - |
UTF8-2*1 | 0xC0 〜 0xDF | UTF8-tail | - | - | - | - |
UTF8-3*2 | 0xE0 〜 0xEF | UTF8-tail | UTF8-tail | - | - | - |
UTF8-4*3 | 0xF0 〜 0xF7 | UTF8-tail | UTF8-tail | UTF8-tail | - | - |
UTF8-5*4 | 0xF8 〜 0xFB | UTF8-tail | UTF8-tail | UTF8-tail | UTF8-tail | - |
UTF8-6*5 | 0xFC 〜 0xFD | UTF8-tail | UTF8-tail | UTF8-tail | UTF8-tail | UTF8-tail |
- UTF8-tail は「0x80 〜 0xBF」
- RFC 2279 - UTF-8, a transformation format of ISO 10646
RFC3629(現)
1バイト | 2バイト | 3バイト | 4バイト | |
---|---|---|---|---|
UTF8-1 | 0x00 〜 0x7F | - | - | - |
UTF8-2*6 | 0xC2 〜 0xDF | UTF8-tail | - | - |
UTF8-3 | 0xE0 | 0xA0 〜 0xBF | UTF8-tail | - |
= | 0xE1 〜 0xEC | UTF8-tail | UTF8-tail | - |
= | 0xED | 0x80 〜 0x9F | UTF8-tail | - |
= | 0xEE 〜 0xEF | UTF8-tail | UTF8-tail | - |
UTF8-4 | 0xF0 | 0x90 〜 0xBF | UTF8-tail | UTF8-tail |
= | 0xF1 〜 0xF3 | UTF8-tail | UTF8-tail | UTF8-tail |
= | 0xF4 | 0x80 〜 0x8F | UTF8-tail | UTF8-tail |
- UTF8-tail は「0x80 〜 0xBF」
- 5〜6バイト表現は全て撤廃
- RFC 3629 - UTF-8, a transformation format of ISO 10646
このチェックロジック作るの面倒だなァ〜…
参照
Google Chrome 4.0 (BETA) へupdate
参照
今朝通勤中に電車でコソコソ見ました…
はやく病院行ってください…
外部JavaScriptファイルでformをsubmitする場合の注意 submitが複数回実行される
submit.js
$(function() { var btn = $('.btn'); btn.click(function(){ $(this).parents('form:first').submit(); alert('call alert'); }); });
test.html
<script type="text/javascript" src="jquery.js"></script> <!-- 同じ外部JavaScriptファイルを2回呼び出す --> <script type="text/javascript" src="alert.js"></script> <script type="text/javascript" src="alert.js"></script> <form name="form1" action="test2.html" method="post"> <input class="btn" type="button" value="送信"> </form>
IE8、Google Chrome3.0
- alert.js は2回読み込まれる
- submit は2回実行される
- alert は2回実行される
FireFox3.5
- alert.js は2回読み込まれる
- submit は1回だけ実行される
- alert は1回だけ実行される
まとめ
ブラウザごとに挙動が変わるのでページ内で同じ外部JavaScriptファイルの読み込みが複数回おこないわないように注意する