| 関数名 | 目的 | 結果 |
|---|---|---|
| "aaa".match(/visepaaamks/); | aaaの中で()内の文字列と最初に一致するものを抜き出す | 最初に一致した部分 |
| "bnidtaaanor".search(/aaa/); | bnidtaaanorの文字列の中からaaaと一致するのは前から数えて何番目か | 何番目か(頭は0から数える) |
| /aaa/.test("wodkfgidsoaabis"); | ()の中の文字列にaaaが含まれるか | true false |
| 正規表現 | 意味 | 備考 |
|---|---|---|
| /\w/ | 英数字かアンダーバーの1文字 | マルチバイト文字はfalseになる [a-zA-Z0-9] と同じ意味 |
| /\d/ | 数字の1文字 [0-9] と同じ意味 |
|
| /\s/ | スペースの1文字 | |
| /\W/ | 英数字かアンダーバーでない1文字 | マルチバイト文字はtrueとなる |
| /\D/ | 数字でない1文字 | [^0-9] と同じ意味 |
| /\S/ | スペースでない1文字 | |
| /[abc123]/ | a b c 1 2 3 のどれかを含む | |
| /[a-e]/ | a b c d e のどれかを含む | |
| /[^aaa]/ | aaaでない | |
| /\w{2}/ | 2文字 | \w\w と同じ意味 |
| /\w{2,}/ | 2文字以上 | \w\w...... と同じ意味 |
| /\w{2,3}/ | 2文字以上3文字以下 | \w\w か \w\w\w と同じ意味 |
| /\w?/ | 0個以上1個以下の文字 | |
| /\w+/ | 1個以上の文字 | \w..... と同じ意味 |
| /\w*/ | 0個以上の文字 | \w{0,} と同じ意味 |
| /\d{3}-\d{4}/ | 3桁の数字、ハイフン、4桁の数字 | \d\d\d - \d\d\d\d と同じ意味 郵便番号など |
| /(c | d)?/ | cかdのどちらかが0個以上1個以下 | |
| /(c | d){2,}/ | cかdが2回以上 | cc dd cd dc 順番は関係ない |
| /Code(Study)?/ | Code に続けて Study が0個以上1個以下 | |
| /Code\s(Study){3}/ | Code スペース に続けて Study が3個 | |
| /^abc/ | abc で始まる | |
| /xyz$/ | xyz で終わる | |
| /./ | 任意の1文字 | |
| /.+/ | 任意の1文字以上 | |
| /.{5}/ | 任意の5文字 | |
| /\./ | ドット | |
| /a.{5}c/ | aに続き、任意の5文字、cで終わる | |
| /a(.{5})c/ | aに続き、任意の5文字、cで終わる | match関数の戻り値として2番目のインデックスに(.{5})が入る。 |
| /\d+/ | 1桁以上の数字 |
| ソースコードの変数名 | 答え | 問題 | 補足 |
|---|---|---|---|
| result1 | "CodeStudy".match(/Study/); | ||
| result2 | |||
| result3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | |||
| 31 |
var result1 = "CodeStudy".match(/Study/); // /Study/に当たるものを"CodeStudyから抜き出す"
document.getElementById('1').innerHTML = result1;
var result2 = /Study/.test("CodeStudy"); // CodeStudyの中に/Study/を含んでいるか
document.getElementById('2').innerHTML = result2;
var result3 = "CodeStudy".search(/Study/); // CodeStudyの中から/Study/を探す(何番目に含まれているか)
document.getElementById('3').innerHTML = result3;
var result4 = /[12345]/.test(34); // 34の中に1~5のどれかを含んでいるか
document.getElementById('4').innerHTML = result4;
var result5 = /[^abcdefghijklmnopqrstuvwxyz]/.test(34); //34の中にa~zでないものを含んでいるか
document.getElementById('5').innerHTML = result5;
var result6 = /\W/.test(" "); // 空白の中に文字でないものは含まれているか
document.getElementById('6').innerHTML = result6;
var result7 = /\s/.test("Wataru Adachi"); // Wataru Adachiの中に空白があるか
document.getElementById('7').innerHTML = result7;
var result8 = /\D/.test("wataru"); // wataruの中に数字でないものが含まれているか
document.getElementById('8').innerHTML = result8;
var result9 = /[a-e]/.test("wataru"); // wataruの中にaからeを含んでいるか
document.getElementById('9').innerHTML = result9;
var result10 = /[^a-z0-9_]/.test("WATARU1127"); // wataru1127の中に
document.getElementById('10').innerHTML = result10;
var result11 = "wataru1127".match(/\w{4}\d{4}/); // 文字4桁 数字4桁に当たるものをwataru1127から抜き出す
document.getElementById('11').innerHTML = result11;
var result12 = "12 34".match(/\d{2}\s\d{2}/); // 数字2桁 空白 数字2桁に当たるものを12 34から抜き出す
document.getElementById('12').innerHTML = result12;
var result13 = /\w{3}\s?\d+/.test("abc 12"); // 文字3桁 空白0または1個 数字1桁以上
document.getElementById('13').innerHTML = result13;
var result14 = /\d{3}-\d{4}\s*\w/.test("1234"+"a"); // 数字3~4桁 空白なし 文字1桁
document.getElementById('14').innerHTML = result14;
var result15 = /\d{3}-\d{4}/.test("458-0003"); // 数字3桁ハイフン数字4桁
document.getElementById('15').innerHTML = result15;
var result16 = /\d{3,4}/.test("12abc34"); // 3~4連続の数字があるか
document.getElementById('16').innerHTML = result16;
var result17 = /\s*\w/.test(" "); // 空白の有無に関わらず文字・数字があればtrue
document.getElementById('17').innerHTML = result17;
var result18 = /(Code|Study){2}/.test("CodeCode"); // CodeかStudyを2回
document.getElementById('18').innerHTML = result18;
var result19 = /(Code|Study){2}/.test("CodeStudy"); // CodeかStudyを2回
document.getElementById('19').innerHTML = result19;
var result20 = /(Code|Study){2}/.test("StudyCode"); // CodeかStudyを2回
document.getElementById('20').innerHTML = result20;
var result21 = /(Code|Study){2,}/.test("StudyCodeStudy"); // CodeかStudyを2回以上
document.getElementById('21').innerHTML = result21;
var result22 = /Code\s(Study){3}/.test("Code StudyStudyStudy"); // Code、空白、に続いてStudy3回
document.getElementById('22').innerHTML = result22;
var result23 = /(Code)+(Study)?/.test("Code"); // Code1回以上Study0回以上1回以下
document.getElementById('23').innerHTML = result23;
var result24 = /(Code)+(Study)?/.test("Study"); // Code1回以上Study0回以上1回以下
document.getElementById('24').innerHTML = result24;
var result25 = /Code(Study)?/.test("Study"); // Codeに続いて、Studyを0回以上1回以下
document.getElementById('25').innerHTML = result25;
var result26 = /\d+(Study)\s$/.test("12345Study "); // 数字1回以上、Study、空白で終わる
document.getElementById('26').innerHTML = result26;
var result27 = /^Study(\w\d){3,}$/.test("Studya1b2c3"); // 始まりがStudy、1文字1数字1文字1数字1文字1数字で終わる
document.getElementById('27').innerHTML = result27;
var result28 = /^abc\d/.test("a1"); // 始まりがabc、数字1回
document.getElementById('28').innerHTML = result28;
var result29 = /abc\dxyz$/.test("a1xyz"); //abcに続いて、数字1回、xyzで終わる
document.getElementById('29').innerHTML = result29;
var result30 = /\w{3}/.test("あああ"); // 日本語は文字に含まれない
document.getElementById('30').innerHTML = result30;
var result31 = /(c|d)?/.test(" ");
document.getElementById('31').innerHTML = result31;