web-dev-qa-db-ja.com

文字列の特殊文字を確認してください

文字列に!@#$%^&*。、<>/\ '";:?のような特殊文字が含まれているかどうかを確認し、文字列に少なくとも1つの文字が含まれている場合はtrueを返します。

私は次の正規表現、スクリプトで試しました:

var format = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/;

if( string.match(format) ){
  return true;
}else{
  return false;
}

文字列に特殊文字のみが含まれる場合はtrueを返しますが、文字列に英数字(!example1、.example2)などの何かが含まれる場合はfalseを返します。

26
Petru Lebada

RegExp .test()関数を使用してパターン一致をチェックすることをお勧めします。変更する必要があるのは、正規表現の行アンカーの開始/終了(および*量指定子も不要)を削除することだけです。

var format = /[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
//            ^                                    ^   
document.write(format.test("My@string-with(some%text)") + "<br/>");
document.write(format.test("My string with spaces") + "<br/>");
document.write(format.test("MyStringContainingNoSpecialChars"));

アンカー(^文字列/行の開始、$ end od文字列/行、および\bワード境界など)は、文字列内の特定の場所での一致を制限できます。 ^を使用する場合、正規表現エンジンは次のサブパターンが文字列の先頭に表示されるかどうかを確認します(または/m修飾子が正規表現で宣言されている場合は行)。 $の場合と同じ:前のサブパターンは、文字列の最後で一致する必要があります。

あなたの場合、文字列内のセットanywhereからの特殊文字の存在を確認したいです。たとえ1つだけであっても、falseを返します。したがって、アンカーと量指定子*を削除する必要があります。 *量指定子は空の文字列と一致するため、少なくとも1つの特殊文字の存在を実際に確認するために削除する必要があります(実際には、確認する量指定子なしでexactly 1回、同じ{1}制限数量詞を使用しているかのように)。

より具体的な解決策

あなたにとって「特別な」キャラクターとは?

  • ASCII文字以外のすべての文字:/[^\x00-\x7F]/demo
  • 印刷可能なASCII文字以外のすべての文字:/[^ -~]/demo
  • スペース、文字、数字以外の印刷可能なASCII文字:/[!-\/:-@[-`{-~]/(- demo
  • 任意のUnicode 適切な句読点 chars、\p{P} Unicodeプロパティクラス:
    • ECMAScript 2018:/\p{P}/u
    • ES6 +:
/ [!-#%-*、-\ /:;?@ [-\] _ {}\u00A1\u00A7\u00AB\u00B6\u00B7\u00BB\u00BF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u0D\u07F7-\u07F0\u07F0\u07F0\u07F9\u07F9\u07F9\u07F9\u07F0\U07F9\u07F9\u07F0\U07D\u0964\u0965\u0970\u09FD\u0A76\u0AF0\u0C84\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0F0A\u0FD0\u0FD0\u0FD0\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A\u1A\E u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u20u\u2053-\u205E\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E4E\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uD4A\uAFF -\ uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\u A8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE19\uFE19u\uFE19u\uFE19u\uFE19u\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFFFD u {10100}-\ u {10102}\u {1039F}\u {103D0}\u {1056F}\u {10857}\u {1091F}\u {1093F}\u {10A50}-\ u {10A58 }\u {10A7F}\u {10AF0}-\ u {10AF6}\u {10B39}-\ u {10B3F}\u {10B99}-\ u {10B9C}\u {10F55}-\ u {10F59}\u {11047}-\ u {1104D}\u {110BB}\u {110BC}\u {110BE}-\ u {110C1}\u {11140}-\ u {11143}\u {11174}\u {11175}\u {111C5}-\ u {111C8}\u {111CD}\u {111DB}\u {111DD}-\ u {111DF}\u {11238}-\ u {1123D}\u {112A9 }\u {1144B}-\ u {1144F}\u {1145B}\u {1145D}\u {114C6}\u {115C1}-\ u {115D7}\u {11641}-\ u {11643}\u {11660}-\ u {1166C}\u {1173C}-\ u {1173E}\u {1183B}\u {11A3F}-\ u {11A46}\u {11A9A}-\ u {11A9C}\u {11A9E}-\ u {11AA2}\u {11C41}-\ u {11C45}\u {11C70}\u {11C71}\u {11EF7}\u {11EF8}\u {12470}-\ u {12474 }\u {16A6E}\u {16A6F}\u {16AF5}\u {16B37}-\ u {16B3B}\u {16B44}\u {16E97}-\ u {16E9A}\u {1BC9F}\u {1DA87}-\ u {1DA8B}\u {1 E95E}\u {1E95F}]/u

●ES5( デモ ):

/(?:[!-#%-\ *、-\ /:; \?@\[-\] _\{\}\xA1\xA7\xAB\xB6\xB7\xBB\xBF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u0D\u0D\u0D700 u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u09FD\u0A76\u0AF0\u0C84\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F0A\u0F0A3\u0F14A\u0F3A-u u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A \\ 180 u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7-\1u0CC1 -\ u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E4E\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030 \\ 3030\u3030 \\ 3030\\ u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\u A8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE30-\uFE30-\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F\D uD800 [\ uDD00-\uDD02\uDF9F\uDFD0] |\uD801\uDD6F |\uD802 [\ uDC57\uDD1F\uDD3F\uDE50-\uDE58\uDE7F\uDEF0-\uDEF6\uDF39-\uDF3F\uDF99-\uDF9C\|\uD803 [\ uDF55-\uDF59] |\uD804 [\ uDC4D\uDCBB\uDCBC\uDCBE-\uDCC1\uDD40-\uDD43\uDD74\uDD75\uDDC5-\uDDC8\uDDCD\uDDDB\uDDDD-\uDDDF\uDE38-\uDE3D\uDEA9] |\uD805 [\ uDC4B-\uDC4F\uDC5B\uDC5D\uDCC6\uDDC1-\uDDD7\uDE41-\uDE43\uDE60-\uDE6C\uDF3C-\uDF3E] |\uD806 uDC3B\uDE3F-\uDE46\uDE9A-\uDE9C\uDE9E-\uDEA2] |\uD807 [\ uDC41-\uDC45\uDC70\uDC71\uDEF7\uDEF8] |\uD809 [\ uDC70-\uDC74] |\uD81A [\ uDE6E\uDE6F\uDEF5\uDF37-\uDF3B\uDF44] |\uD81B [\ uDE9A] |\uD82F\uDC9F |\uD836 [\ uDE87-\uDE8B] |\uD83A [\ uDD5E\uDD5F])/
  • すべてのUnicode記号(句読点ではない)、\p{S}
    • ECMAScript 2018:/\p{S}/u
    • ES6 +:
/ [$ + ^ `|〜\ u00A2-\u00A6\u00A8\u00A9\u00AC\u00AE-\u00B1\u00B4\u00B8\u00D7\u00F7\u02C2-\u02C5\u02D2-\u02DF\u02E5-\u02EB\u02ED\u02EF-\u02FF\u0375\u0384\u0385\u03F6\u0482\u058D-\u058F\u0606-\u0608\u060B\u060E\u060F\u06DE\u06E9\u06FD\u06FE\u07F6\0909F2\0909F2\09\u09FB\u0AF1\u0B70\u0BF3-\u0BFA\u0C7F\u0D4F\u0D79\u0E3F\u0F01-\u0F03\u0F13\u0F15-\u0F17\u0F1A-\u0F0\F0F1F\u0F34\u0F\F0F1F\u0F34\u0F\F0F\F0F\\ u0FCC\u0FCE\u0FCF\u0FD5-\u0FD8\u109E\u109F\u1390-\u1399\u17DB\u1940\u19DE-\u19FF\u1B61-\u1B6A\u1B74-\u1B7C\u1FBD\u1FBF \\ 1FC u1FCF\u1FDD-\u1FDF\u1FED-\u1FEF\u1FFD\u1FFE\u2044\u2052\u207A-\u207C\u208A-\u208C\u20A0-\u20BF\u2100\u2101\u2103-\u2106\u2108\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2108\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109\u2109 u2116-\u2118\u211E-\u2123\u2125\u2127\u2129\u212E\u213A\u213B\u2140-\u2144\u214A-\u214D\u214F\u218A\u218B\u2190-\u2307\u230C-\u2328\u232B-\u2426\u2440-\u244A\u249C-\u24E9\u2500-\u2767\u2794-\u27C4\u27C7-\u27E5\u27F0-\u2982\u2999-\u29D7\u29DC-\u29FB\u29FE-\u2B73\u2B76-u2B7676\u2B95\u2B98-\u2BC8\u2BCA-\u2BFE\u2CE5-\u2CEA\u2E80-\u 2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u2FF0-\u2FFB\u3004\u3012\u3013\u3020\u3036\u3037\u303E\u303F\u309B\u309C\u3190\u3191\u3196-\u319F\u31C\u319F\u31C\u3200-\u321E\u322A-\u3247\u3250\u3260-\u327F\u328A-\u32B0\u32C0-\u32FE\u3300-\u33FF\u4DC0-\u4DFF\uA490-\uA4C6\uA700-\uA716\uA720\uA721\uA789\uA78A\uA828-\uA82B\uA836-\uA839\uAA77-\uAA79\uAB5B\uFB29\uFBB2-\uFBC1\uFDFC\uFDFD\uFE62\uFE64-\uFE66\uFE69\uFF04\uFF0B\uFF0\FF uFF1E\uFF3E\uFF40\uFF5C\uFF5E\uFFE0-\uFFE6\uFFE8-\uFFEE\uFFFC\uFFFD\u {10137}-\ u {1013F}\u {10179}-\ u {10189}\u {1018C} -\ u {1018E}\u {10190}-\ u {1019B}\u {101A0}\u {101D0}-\ u {101FC}\u {10877}\u {10878}\u {10AC8}\u {1173F}\u {16B3C}-\ u {16B3F}\u {16B45}\u {1BC9C}\u {1D000}-\ u {1D0F5}\u {1D100}-\ u {1D126}\u {1D129 }-\ u {1D164}\u {1D16A}-\ u {1D16C}\u {1D183}\u {1D184}\u {1D18C}-\ u {1D1A9}\u {1D1AE}-\ u {1D1E8}\u {1D200}-\ u {1D241}\u {1D245}\u {1D300}-\ u {1D356}\u {1D6C1}\u {1D6DB}\u {1D6FB}\u {1D715}\u { 1D735}\u {1D74F}\u {1D76F}\u {1D789}\u {1D7A9}\u {1D7C3}\u {1D800}-\ u {1D9FF}\u {1DA37}-\ u {1DA3A}\u {1DA6D}-\ u {1DA74}\u {1DA76}-\ u {1DA83}\u {1DA85}\u {1DA86}\u {1ECAC}\u {1ECB0}\u {1EEF0}\u {1EEF1}\u {1F000}-\ u {1F02B}\u {1F030}-\ u {1F093}\u {1F0A0}-\ u {1F0AE}\u {1F0B1}-\ u {1F0BF}\u {1F0C1}-\ u {1F0CF}\u {1F0D1}-\ u {1F0F5}\u {1F110}-\u {1F16B}\u {1F170}-\ u {1F1AC}\u {1F1E6}-\ u {1F202}\u {1F210}-\ u {1F23B}\u {1F240}-\ u {1F248}\u {1F250}\u {1F251}\u {1F260}-\ u {1F265}\u {1F300}-\ u {1F6D4}\u {1F6E0}-\ u {1F6EC}\u {1F6F0}-\ u {1F6F9}\u {1F700}-\ u {1F773}\u {1F780}-\ u {1F7D8}\u {1F800}-\ u {1F80B}\u {1F810}-\ u {1F847}\u { 1F850}-\ u {1F859}\u {1F860}-\ u {1F887}\u {1F890}-\ u {1F8AD}\u {1F900}-\ u {1F90B}\u {1F910}-\ u { 1F93E}\u {1F940}-\ u {1F970}\u {1F973}-\ u {1F976}\u {1F97A}\u {1F97C}-\ u {1F9A2}\u {1F9B0}-\ u {1F9B9 }\u {1F9C0}-\ u {1F9C2}\u {1F9D0}-\ u {1F9FF}\u {1FA60}-\ u {1FA6D}]/u

●ES5( デモ ):

/(?:[$ + ^ `|〜\ xA2-\xA6\xA8\xA9\xAC\xAE-\xB1\xB4\xB8\xD7\xF7\u02C2-\u02C5\u02D2-\u02DF\u02E5-\u02EB\u02ED\u02EF-\u02FF\u0375\u0384\u0385\u03F6\u0482\u058D-\u058F\u0606-\u0608\u060B\u060E\u060F\u06DE\u06E9\u06FD\u06FE\u07F6\u07FE\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF\u07FF u09F3\u09FA\u09FB\u0AF1\u0B70\u0BF3-\u0BFA\u0C7F\u0D4F\u0D79\u0E3F\u0F01-\u0F03\u0F13\u0F15-\u0F17\00F0F \\ 00F0F\F0F0F\F0F0F\F0F0F\F0F1F\F0F0F0\F0F0F0F3\u0FC7-\u0FCC\u0FCE\u0FCF\u0FD5-\u0FD8\u109E\u109F\u1390-\u1399\u17DB\u1940\u19DE-\u19FF\u1B61-\u1B6A\u1B74-\u1B7C\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FBD\u1FCD u1FCD-\u1FCF\u1FDD-\u1FDF\u1FED-\u1FEF\u1FFD\u1FFE\u2044\u2052\u207A-\u207C\u208A-\u208C\u20A0-\u20BF\u2100\u2101\u2103-\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2106\u2114\u2116-\u2118\u211E-\u2123\u2125\u2127\u2129\u212E\u213A\u213B\u2140-\u2144\u214A-\u214D\u214F\u218A\u218B\u2190-\u2307\u230C-\u2328\u232B-\u2426\u2440-\u244A\u249C-\u24E9\u2500-\u2767\u2794-\u27C4\u27C7-\u27E5\u27F0-\u2982\u2999-\u29D7\u29DC-\u29FB\u29FE-\u2B73\u2B76-\u2B95\u2B98-\u2BC8\u2BCA-\u2BFE\u2CE5-\u2CEA\u2E80-\u2E99\u2E9B-\u2EF3\u 2F00-\u2FD5\u2FF0-\u2FFB\u3004\u3012\u3013\u3020\u3036\u3037\u303E\u303F\u309B\u309C\u3190\u3191\u3196-\u319F\u31C0-\u31E3\u3200-\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u321E\u32 -\ u3247\u3250\u3260-\u327F\u328A-\u32B0\u32C0-\u32FE\u3300-\u33FF\u4DC0-\u4DFF\uA490-\uA4C6\uA700-\uA716\uA720\uA721\uA789\uA78A\uA78A\uA828A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A\uA78A -\ uA82B\uA836-\uA839\uAA77-\uAA79\uAB5B\uFB29\uFBB2-\uFBC1\uFDFC\uFDFD\uFE62\uFE64-\uFE66\uFE69\uFF04\uFF0B\uFF1C-\uFF1E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF3E\uFF1E\uFF5E\uFFE0-\uFFE6\uFFE8-\uFFEE\uFFFC\uFFFD] |\uD800 [\ uDD37-\uDD3F\uDD79-\uDD89\uDD8C-\uDD8E\uDD90-\uDD9B\uDDA0\uDDD0-\uDDFC]\uDDFC]\uD802 [\ uDC77\uDC78\uDEC8] |\uD805\uDF3F |\uD81A [\ uDF3C-\uDF3F\uDF45] |\uD82F\uDC9C |\uD834 [\ uDC00-\uDCF5\uDD00-\uDD26\uDD29-\uDD64\uDD6A-\uDD6C\uDD83\uDD84\uDD8C-\uDDA9\uDDAE-\uDDE8\uDE00-\uDE41\uDE45\uDF00-\uDF56] |\uD835 [\ uDEC1\uDEDB\uDEFB\uDF15\uDF35\uDF4\uDF15\uDF35\uDF4\uDF35\uDF4 uDF6F\uDF89\uDFA9\uDFC3] |\uD836 [\ uDC00-\uDDFF\uDE37-\uDE3A\uDE6D-\uDE74\uDE76-\uDE83\uDE85\uDE86] |\uD83B [\ uDCAC\uDCB0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0\uDEF0 |\uD83C [\ uDC00-\uDC2B\uDC30-\uDC93\uDCA0-\uDCAE\uDCB1-\uDCBF\uDCC1-\uDCCF\u DCD1-\uDCF5\uDD10-\uDD6B\uDD70-\uDDAC\uDDE6-\uDE02\uDE10-\uDE3B\uDE40-\uDE48\uDE50\uDE51\uDE60-\uDE65\uDF00-\uDFFF] |\uD83D [\ uDC00 -\ uDED4\uDEE0-\uDEEC\uDEF0-\uDEF9\uDF00-\uDF73\uDF80-\uDFD8] |\uD83E [\ uDC00-\uDC0B\uDC10-\uDC47\uDC50-\uDC59\uDC60-\uDC87\uDC90 -\ uDCAD\uDD00-\uDD0B\uDD10-\uDD3E\uDD40-\uDD70\uDD73-\uDD76\uDD7A\uDD7C-\uDDA2\uDDB0-\uDDB9\uDDC0-\uDDC2\uDDD0-\uDDFF\uDE60-uDE60-uDE60-uDE60-uDE60-uDE60- ])/
  • すべてのUnicode句読点と記号、\p{P}および\p{S}
    • ECMAScript 2018:/[\p{P}\p{S}]/u
    • ES6 +:
/ [!-\ /:-@ [-`{-〜\ u00A1-\u00A9\u00AB\u00AC\u00AE-\u00B1\u00B4\u00B6-\u00B8\u00BB\u00BF\u00D7\u00F7\u02C2-\u02C5\u02D2-\u02DF\u02E5-\u02EB\u02ED\u02EF-\u02FF\u0375\u037E\u0384\u0385\u0387\u03F6\u0482\u055A-\u055F\u0589\u058A\u058D\u058F\u0C\u05C6\u05F3\u05F4\u0606-\u060F\u061B\u061E\u061F\u066A-\u066D\u06D4\u06DE\u06E9\u06FD\u06FE\u0700-\u070D\u07F \\ u07F9\u07FE\u07\6-07 u083E\u085E\u0964\u0965\u0970\u09F2\u09F3\u09FA\u09FB\u09FD\u0A76\u0AF0\u0AF1\u0B70\u0BF3-\u0BFA\u0C7F\u0E5E0E\E0E0u\E0E\E0E\u0D79\u0D4E\u0D7\\ u0F01-\u0F17\u0F1A-\u0F1F\u0F34\u0F36\u0F38\u0F3A-\u0F3D\u0F85\u0FBE-\u0FC5\u0FC7-\u0FCC\u0FCE-\u0FDA\u104F-u\F104\-109 u1360-\u1368\u1390-\u1399\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DB\u1800-\u180A\u1940\u1944\u1945\u19DE-\u19FF\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B6A\u1B74-\u1B7C\u1BFC-\u1BFF\u1C3 \\ 1c3F\u1C7 \\ u1C3F\u1C7U\C u1FBD\u1FBF-\u1FC1\u1FCD-\u1FCF\u1FDD-\u1FDF\u1FED-\u1 FEF\u1FFD\u1FFE\u2010-\u2027\u2030-\u205E\u207A-\u207E\u208A-\u208E\u20A0-\u20BF\u2100\u2101\u2103-\u2106\u2108\u2109\u2114\u2116-\u2118\u211E-\u2123\u2125\u2127\u2129\u212E\u213A\u213B\u2140-\u2144\u214A-\u214D\u214F\u218A\u218B\u2190-\u2426\u2440-\u244A\u249C-\u24E9\u2500 - u2E80-\u2E99\u2E9B-\u2EF3\u2F00-\u2FD5\u2FF0-\u2FFB\u3001-\u3004\u3008-\u3020\u3030\u3036\u3037\u303D-\u303F\u309B\u309C\u30A0\u30U190uu\u3191\u3196-\u319F\u31C0-\u31E3\u3200-\u321E\u322A-\u3247\u3250\u3260-\u327F\u328A-\u32B0\u32C0-\u32FE\u3300-\u33FF\u4DC0-\u4DFF\uA490-\uA4C6\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA700-\uA716\uA720\uA721\uA789\uA78A\uA828-\uA82B\uA836-\A8u\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAA77-\uAA79\uAADE\uAADF\uAAF\uAAB\uAAF\AB\AB\uFBB2-\uFBC1\uFD3E\uFD3F\uFDFC\uFDFD\uFE10-\uFE 19\uFE30-\uFE52\uFE54-\uFE66\uFE68-\uFE6B\uFF01-\uFF0F\uFF1A-\uFF20\uFF3B-\uFF40\uFF5B-\uFF65\uFFE0-\uFFE6\uFFE8-\uFFEE\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\uFFFC\u {10100}-\ u {10137}-\ u {1013F}\u {10179}-\ u {10189}\u {1018C}-\ u {1018E}\u {10190}-\u {1019B}\u {101A0}\u {101D0}-\ u {101FC}\u {1039F}\u {103D0}\u {1056F}\u {10857}\u {10877}\u {10878 }\u {1091F}\u {1093F}\u {10A50}-\ u {10A58}\u {10A7F}\u {10AC8}\u {10AF0}-\ u {10AF6}\u {10B39}-\ u {10B3F}\u {10B99}-\ u {10B9C}\u {10F55}-\ u {10F59}\u {11047}-\ u {1104D}\u {110BB}\u {110BC}\u { 110BE}-\ u {110C1}\u {11140}-\ u {11143}\u {11174}\u {11175}\u {111C5}-\ u {111C8}\u {111CD}\u {111DB}\u {111DD}-\ u {111DF}\u {11238}-\ u {112A9}\u {1144B}-\ u {1144F}\u {1145B}\u {1145D}\u {114C6}\u {115C1}-\ u {115D7}\u {11641}-\ u {11643}\u {11660}-\ u {1166C}\u {1173C}-\ u {1173F}\u { 1183B}\u {11A3F}-\ u {11A46}\u {11A9A}-\ u {11A9C}\u {11A9E}-\ u {11AA2}\u {11C41}-\ u {11C45}\u {11C70 }\u {11C71}\u {11EF7}\u {11EF8}\u {12470}-\ u {12474}\u {16A6E}\u {16A6F}\u {16AF5}\u {16B37}-\ u {16B3F}\u {16B44}\u {16B45}\u {16E97}-\ u {16E9A}\u {1BC9C}\u {1BC9F}\u {1D000}-\ u {1D0F5}\u {1D100}-\ u {1D126}\u {1D129}-\ u {1D164}\u {1D16A}-\ u {1D16C}\u {1D183}\u {1D184}\u {1D18C}-\ u { 1D1A9}\u {1D1AE}-\ u {1D1E8}\u {1D200}-\ u {1D241}\u {1D245}\u {1D300}-\ u {1D356}\u {1D6C1}\u {1D6DB}\u {1D6FB}\u {1D715}\u {1D735}\u {1D74F}\u {1D76F}\u {1D789}\u {1D7A9}\u {1D7C3}\u {1D800}-\ u {1D9FF }\u {1DA37}-\ u {1DA3A}\u {1DA6D}-\ u {1DA74}\u {1DA76}-\ u {1DA83}\u {1DA85}-\ u {1DA8B}\u {1E95E}\u {1E95F}\u {1ECAC}\u {1ECB0}\u {1EEF0}\u {1EEF1}\u {1F000}-\ u {1F02B}\u {1F030}-\ u {1F093}\u { 1F0A0}-\ u {1F0AE}\u {1F0B1}-\ u {1F0BF}\u {1F0C1}-\ u {1F0CF}\u {1F0D1}-\ u {1F0F5}\u {1F110}-\ u { 1F16B}\u {1F170}-\ u {1F1AC}\u {1F1E6}-\ u {1F202}\u {1F210}-\ u {1F23B}\u {1F240}-\ u {1F248}\u {1F250 }\u {1F251}\u {1F260}-\ u {1F265}\u {1F300}-\ u {1F6D4}\u {1F6E0}-\ u {1F6EC}\u {1F6F0}-\ u {1F6F9}\u {1F700}-\ u {1F773}\u {1F780}-\ u {1F7D8}\u {1F800}-\ u {1F80B}\u {1F810}-\ u {1F847}\u {1F850}-\u {1F859}\u {1F860}-\ u {1F887}\u {1F890}-\ u {1F8AD}\u {1F900}-\ u {1F90B}\u {1F910}-\ u {1F93E}\u {1F940}-\ u {1F970}\u {1F973}-\ u {1F976}\u {1F97A}\u {1F97C}-\ u {1F9A2}\u {1F9B0}-\ u {1F9B9}\u {1F9C0}-\ u {1F9C2}\u {1F9D0}-\ u {1F9FF}\u {1FA 60}-\ u {1FA6D}]/u

●ES5( デモ ):

/(?:[!-\ /:-@\[-`\ {-〜\ xA1-\xA9\xAB\xAC\xAE-\xB1\xB4\xB6-\xB8\xBB\xBF\xD7\xF7\u02C2-\u02C5\u02D2-\u02DF\u02E5-\u02EB\u02ED\u02EF-\u02FF\u0375\u037E\u0384\u0385\u0387\u03F6\u0482\u055A-\u055F\u0589\u058A\u058D-u\58 u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0606-\u060F\u061B\u061E\u061F\u066A-\u066D\u06D4\u06DE\u06E9\u06FD\u07F\u07F\u07F\u07F\u07F\u07F\u07F\u07F\u07F\u07F\u07F\u07F6-u07F\u07D u07FF\u0830-\u083E\u085E\u0964\u0965\u0970\u09F2\u09F3\u09FA\u09FB\u09FD\u0A76\u0AF0\u0AF1\u0B70\u0BF3-\u0BFA\u0C7\00D4 u0E4F\u0E5A\u0E5B\u0F01-\u0F17\u0F1A-\u0F1F\u0F34\u0F36\u0F38\u0F3A-\u0F3D\u0F85\u0FBE-\u0FU\u0FCE\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u0FCC\u109F\u10FB\u1360-\u1368\u1390-\u1399\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DB\u1800-\u180A\u1940\u1944\u1945\u19DE-\u19FF\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B6A\u1B74-\u1B7C\u1BFC-\u1B3\7C1F7\C\u1CC7\u1CD3\u1FBD\u1FBF-\u1FC1\u1FCD-\u1FCF\u1FDD-\u1FDF\u1FED-\u1FEF\u1FFD\u1FFE\u2010 -\ u2027\u2030-\u205E\u207A-\u207E\u208A-\u208E\u20A0-\u20BF\u2100\u2101\u2103-\u2106\u2108\u2109\u2114\u2116-\u2118\u211E-\u2123\u2125\u2127\u2129\u212E\u213A\u213B\u2140-\u2144\u214A-\u214D\u214F\u218A\u218B\u2190-\u2426\u2440-\u244A\u249C-\u24E9\u2500-\u2775\u2794-\u2B73\u2B76-\u2B95\u2B98-\u2BC8\u2BCA-\u2BFE\u2CE5\u2EF3\u2F00-\u2FD5\u2FF0-\u2FFB\u3001-\u3004\u3008-\u3020\u3030\u3036\u3037\u303D-\u303F\u309B\u309C\u30A0\u30FB\u3190\u3191\u3196-\319319\u31C0-\u31E3\u3200-\u321E\u322A-\u3247\u3250\u3260-\u327F\u328A-\u32B0\u32C0-\u32FE\u3300-\u33FF\u4DC0-\u4DFF\uA490-\uA4C6\uA4\6A uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA700-\uA716\uA720\uA721\uA789\uA78A\uA828-\uA82B\uA836-\uA839\uA874-\uA877\uA8\uA8\uA8\uA8\uA8\uA8\uA8\\ uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAA77-\uAA79\uAADE\uAADF\uAAF0\uAAF1\uAB5B\uABEB\uFB29\uFB29\uFB29\uFB29\uFB29\uAB29\uFB29\uAB29\uFB29\uFD3F\uFDFC\uFDFD\uFE10-\uFE19\uFE30-\uFE52\uFE54 -\ uFE66\uFE68-\uFE6B\uFF01-\uFF0F\uFF1A-\uFF20\uFF3B-\uFF40\uFF5B-\uFF65\uFFE0-\uFFE6\uFFE8-\uFFEE\uFFFC\uFFFD] |\uD800 [\ uDD00-\uDD02\uDD37-\uDD3F\uDD79-\uDD89\uDD8C-\uDD8E\uDD90-\uDD9B\uDDA0\uDDD0-\uDDFC\uDF9F\uDFD0] |\uD801\uDD6F |\uD802 [\ uDC57\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77\uDC77 uDD1F\uDD3F\uDE50-\uDE58\uDE7F\uDEC8\uDEF0-\uDEF6\uDF39-\uDF3F\uDF99-\uDF9C] |\uD803 [\ uDF55-\uDF59] |\uD804 [\ uDC47-\uDC4D\uDCBB\uDCBC\uDCBE-\uDCC1\uDD40-\uDD43\uDD74\uDD75\uDDC5-\uDDC8\uDDCD\uDDDB\uDDDD-\uDDDF\uDE38-\uDE3D\uDEA9] |\uD805 [\ uDC4B-\uDC4F\uDC5B\uDC5B\uDC5B\uDC5B\uDC5B\uDC5B\uDC5B\uDC5B\uDC5B\uDC5B\uDC5F\uDCC6\uDDC1-\uDDD7\uDE41-\uDE43\uDE60-\uDE6C\uDF3C-\uDF3F] |\uD806 [\ uDC3B\uDE3F-\uDE46\uDE9A-\uDE9C\uDE9E-\uDEA2] \\ D uDC41-\uDC45\uDC70\uDC71\uDEF7\uDEF8] |\uD809 [\ uDC70-\uDC74] |\uD81A [\ uDE6E\uDE6F\uDEF5\uDF37-\uDF3F\uDF44\uDF45] |\uD81B [\ uDE97-\uDE9A] |\uD82F [\ uDC9C\uDC9F] |\uD834 [\ uDC00-\uDCF5\uDD00-\uDD26\uDD29-\uDD64\uDD6A-\uDD6C\uDD83\uDD84\uDD8C-\uDDA9\uDDAE-\uDE\uDE00-\uDE41\uDE45\uDF00-\uDF56] |\uD835 [\ uDEC1\uDEDB\uDEFB\uDF15\uDF35\uDF4F\uDF6F\uDF89\uDFA9\uDFC3] |\uD836 [\uDC00-\uDDFF\uDE37-\uDE3A\uDE6D-\uDE74\uDE76-\uDE83\uDE85-\uDE8B] |\uD83A [\ uDD5E\uDD5F] |\uD83B [\ uDCAC\uDCB0\uDEF0\uDEF1] |\uD83C [\ uDC00-\uDC2B\uDC30-\uDC93\uDCA0-\uDCAE\uDCB1-\uDCBF\uDCC1-\uDCCF\uDCD1-\uDCF5\uDD10-\uDD6B\uDD70-\uDDAC\uDDE6-\uDE02\uDE10-\uDE3B\uDE40-\uDE48\uDE50\uDE51\uDE60-\uDE65\uDF00-\uDFFF] |\uD83D [\ uDC00-\uDED4\uDEE0-\uDEEC\uDEF0-\uDEF9\uDF00-\uDF73\uDF80-\uDFD8] |\uD83E [\ uDC00-\uDC0B\uDC10-\uDC47\uDC50-\uDC59\uDC60-\uDC87\uDC90-\uDCAD\uDD00-\uDD0B\uDD10-\uDD3E\uDD40-\uDD70\uDD73-\uDD76\uDD7A\uDD7C-\uDDA2\uDDB0-\uDDB9\uDDC0-\uDDC2\uDDD0-\uDDFF\uDE60-\uDE6D])/
34

正規表現は^$を使用するため、文字列全体と一致するように試みます。結果としてブール値のみが必要な場合は、testの代わりにmatchを使用します。

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;

if(format.test(string)){
  return true;
} else {
  return false;
}
10
Magus

代わりに、英数字を否定一致させる方が簡単ではないでしょうか?

return string.match(/^[^a-zA-Z0-9]+$/) ? true : false;
4
Edson Medina
var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=";
var checkForSpecialChar = function(string){
 for(i = 0; i < specialChars.length;i++){
   if(string.indexOf(specialChars[i]) > -1){
       return true
    }
 }
 return false;
}

var str = "YourText";
if(checkForSpecialChar(str)){
  alert("Not Valid");
} else {
    alert("Valid");
}
1
sAcH

正規表現から文字^(文字列の先頭)および$(文字列の末尾)を削除します。

var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/;
1

これを試すことができます:

regex = [\W_]

それは間違いなくあなたを助けます。

0
Mayur Koshti