Webアプリケーションでアスキーの特殊文字32文字を入力したら、「特殊文字は入力不可」と表示させる。
このようなことをしたい場合は、32個のアスキー特殊文字を表す正規表現オブジェクトを生成する必要がある。
この32文字の特殊文字を表す正規表現を1文字づつ書くと長くなる。
しかし、32個のアスキー特殊文字を正規表現クラス[]を用いて文字範囲を指定して短く書くことができる。
!から/まで文字の範囲を指定する。
:から@まで文字の範囲を指定する。
[から`まで文字の範囲を指定する。
{から˜まで文字の範囲を指定する。
これにより短い正規表現で表せる。
[!-\/:-@[-`{-~]
/*
32個のアスキー特殊文字
! " # $ % & ' ( ) * + , - . / :; < = > ? @ [ \ ] ^ _ ` { | } ~
JavaScript:the good parts P87は間違えているな。
オライリーのJavaScript:the good partsの正誤表には書いていない。
10年以上前に買った本だけど、2023年3月2日にこの正規表現が間違っていることに気づいた。
全く気付かなかったわ。
(誤)?が抜けているな。
(?:!|"|#|\$|%|&|'|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|@|\[|\\|]|\^|_|`|\{|\||\}|~)
(正)?を追加した。
(?:!|"|#|\$|%|&|'|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|@|\[|\\|]|\^|_|`|\{|\||\}|~)
*/
let regexp = /(?:!|"|#|\$|%|&|'|\(|\)|\*|\+|,|-|\.|\/|:|;|<|=|>|\?|@|\[|\\|]|\^|_|`|\{|\||\}|~)/;
let regexp2 = /[!-\/:-@\[-`{-~]/;
let searchWord = [];
searchWord.push("!");
searchWord.push('"');
searchWord.push("#");
searchWord.push("\$");
searchWord.push("%");
searchWord.push("&");
searchWord.push("'");
searchWord.push("\(");
searchWord.push("\)");
searchWord.push("\*");
searchWord.push("\+");
searchWord.push(",");
searchWord.push("-");
searchWord.push("\.");
searchWord.push("\/");
searchWord.push(":");
searchWord.push(";");
searchWord.push("<");
searchWord.push("=");
searchWord.push(">");
searchWord.push("?");
searchWord.push("@");
searchWord.push("\[");
searchWord.push("\\");
searchWord.push("]");
searchWord.push("\^");
searchWord.push("_");
searchWord.push("`");
searchWord.push("\{");
searchWord.push("\|");
searchWord.push("\}");
searchWord.push("~");
searchWord.push("a");
searchWord.push("b");
searchWord.push("c");
searchWord.push("d");
searchWord.push("e");
searchWord.push("f");
searchWord.push("g");
searchWord.push("h");
searchWord.push("i");
searchWord.push("j");
searchWord.push("k");
searchWord.push("l");
searchWord.push("m");
searchWord.push("n");
searchWord.push("o");
searchWord.push("p");
searchWord.push("q");
searchWord.push("r");
searchWord.push("s");
searchWord.push("t");
searchWord.push("u");
searchWord.push("v");
searchWord.push("w");
searchWord.push("x");
searchWord.push("y");
searchWord.push("z");
searchWord.push("0");
searchWord.push("1");
searchWord.push("2");
searchWord.push("3");
searchWord.push("4");
searchWord.push("5");
searchWord.push("6");
searchWord.push("7");
searchWord.push("8");
searchWord.push("9");
console.log("searchWord.length = " + searchWord.length)
searchWord.forEach((element, i) => {
console.log("searchWord["+i+"] = "+element+" searchWord["+i+"] = " + regexp.test(element) +", searchWord["+i+"] = "+regexp2.test(element) );
});
/*
searchWord.length = 68
searchWord[0] = ! searchWord[0] = true, searchWord[0] = true
searchWord[1] = " searchWord[1] = true, searchWord[1] = true
searchWord[2] = # searchWord[2] = true, searchWord[2] = true
searchWord[3] = $ searchWord[3] = true, searchWord[3] = true
searchWord[4] = % searchWord[4] = true, searchWord[4] = true
searchWord[5] = & searchWord[5] = true, searchWord[5] = true
searchWord[6] = ' searchWord[6] = true, searchWord[6] = true
searchWord[7] = ( searchWord[7] = true, searchWord[7] = true
searchWord[8] = ) searchWord[8] = true, searchWord[8] = true
searchWord[9] = * searchWord[9] = true, searchWord[9] = true
searchWord[10] = + searchWord[10] = true, searchWord[10] = true
searchWord[11] = , searchWord[11] = true, searchWord[11] = true
searchWord[12] = - searchWord[12] = true, searchWord[12] = true
searchWord[13] = . searchWord[13] = true, searchWord[13] = true
searchWord[14] = / searchWord[14] = true, searchWord[14] = true
searchWord[15] = : searchWord[15] = true, searchWord[15] = true
searchWord[16] = ; searchWord[16] = true, searchWord[16] = true
searchWord[17] = < searchWord[17] = true, searchWord[17] = true
searchWord[18] = = searchWord[18] = true, searchWord[18] = true
searchWord[19] = > searchWord[19] = true, searchWord[19] = true
searchWord[20] = ? searchWord[20] = true, searchWord[20] = true
searchWord[21] = @ searchWord[21] = true, searchWord[21] = true
searchWord[22] = [ searchWord[22] = true, searchWord[22] = true
searchWord[23] = \ searchWord[23] = true, searchWord[23] = true
searchWord[24] = ] searchWord[24] = true, searchWord[24] = true
searchWord[25] = ^ searchWord[25] = true, searchWord[25] = true
searchWord[26] = _ searchWord[26] = true, searchWord[26] = true
searchWord[27] = ` searchWord[27] = true, searchWord[27] = true
searchWord[28] = { searchWord[28] = true, searchWord[28] = true
searchWord[29] = | searchWord[29] = true, searchWord[29] = true
searchWord[30] = } searchWord[30] = true, searchWord[30] = true
searchWord[31] = ~ searchWord[31] = true, searchWord[31] = true
searchWord[32] = a searchWord[32] = false, searchWord[32] = false
searchWord[33] = b searchWord[33] = false, searchWord[33] = false
searchWord[34] = c searchWord[34] = false, searchWord[34] = false
searchWord[35] = d searchWord[35] = false, searchWord[35] = false
searchWord[36] = e searchWord[36] = false, searchWord[36] = false
searchWord[37] = f searchWord[37] = false, searchWord[37] = false
searchWord[38] = g searchWord[38] = false, searchWord[38] = false
searchWord[39] = h searchWord[39] = false, searchWord[39] = false
searchWord[40] = i searchWord[40] = false, searchWord[40] = false
searchWord[41] = j searchWord[41] = false, searchWord[41] = false
searchWord[42] = k searchWord[42] = false, searchWord[42] = false
searchWord[43] = l searchWord[43] = false, searchWord[43] = false
searchWord[44] = m searchWord[44] = false, searchWord[44] = false
searchWord[45] = n searchWord[45] = false, searchWord[45] = false
searchWord[46] = o searchWord[46] = false, searchWord[46] = false
searchWord[47] = p searchWord[47] = false, searchWord[47] = false
searchWord[48] = q searchWord[48] = false, searchWord[48] = false
searchWord[49] = r searchWord[49] = false, searchWord[49] = false
searchWord[50] = s searchWord[50] = false, searchWord[50] = false
searchWord[51] = t searchWord[51] = false, searchWord[51] = false
searchWord[52] = u searchWord[52] = false, searchWord[52] = false
searchWord[53] = v searchWord[53] = false, searchWord[53] = false
searchWord[54] = w searchWord[54] = false, searchWord[54] = false
searchWord[55] = x searchWord[55] = false, searchWord[55] = false
searchWord[56] = y searchWord[56] = false, searchWord[56] = false
searchWord[57] = z searchWord[57] = false, searchWord[57] = false
searchWord[58] = 0 searchWord[58] = false, searchWord[58] = false
searchWord[59] = 1 searchWord[59] = false, searchWord[59] = false
searchWord[60] = 2 searchWord[60] = false, searchWord[60] = false
searchWord[61] = 3 searchWord[61] = false, searchWord[61] = false
searchWord[62] = 4 searchWord[62] = false, searchWord[62] = false
searchWord[63] = 5 searchWord[63] = false, searchWord[63] = false
*/
参考
JavaScript:the good parts 「良いパーツ」によるベストプラクティス [ ダグラス・クロフォード ] ExternalLink P86~87