開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)の Part 1(Starting with the basics)、Chapter 7(Strings)、Building, your Regex Patterns の Exercises on Regexes.を JavaScript で取り組んでみる。
Exercises on Regexes.
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample_regexes.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'), btn1 = document.querySelector('#clear0'), pre0 = document.querySelector('#output0'), p = (x) => pre0.textContent += x + '\n'; let output = () => { p('10 digits'); let regex = /\d{10}/; p(regex.exec('a1234567890bc')[0]); p(regex.exec('a123456789bc')); p('valid octal number'); regex = /^[0-7]+$/; p(regex.exec('')); p(regex.exec('0')[0]); p(regex.exec('01234567')[0]); p(regex.exec('012345678')); p('first word'); regex = /\w+/; p(regex.exec('think perl 6')[0]); p(regex.exec('think')[0]); p('first word(starting with an "a")'); regex = /\ba\w*/; p(regex.exec('b')); p(regex.exec('like alike')[0]); p(regex.exec('alike like')[0]); p('first word(starting with a lowercase vowel)'); regex = /\b[aeiou]\w*/; p(regex.exec('b')); p(regex.exec('e')[0]); p(regex.exec('a e')[0]); p(regex.exec('b e')[0]); p(regex.exec('epub')[0]); p(regex.exec('book epub')[0]); p('French mobile telephone nuber.'); regex = /^0[67]\d{8}$/; p(regex.exec('0612345678')[0]); p(regex.exec('061234567')); p(regex.exec('06123456789')); p('first word(starting with a vowel)'); regex = /\b[aeiou]\w*/i; p(regex.exec('b')); p(regex.exec('e')[0]); p(regex.exec('a e')[0]); p(regex.exec('b e')[0]); p(regex.exec('epub')[0]); p(regex.exec('book epub')[0]); p(regex.exec('B')); p(regex.exec('E')[0]); p(regex.exec('A E')[0]); p(regex.exec('B E')[0]); p(regex.exec('Epub')[0]); p(regex.exec('BOOK EPUB')[0]); p('second occurence of a double letter'); regex = /(\w)\1.*((\w)\3)/; p(regex.exec('aa bb')[2]); p(regex.exec('aa bbb')[2]); p(regex.exec('aaa bbb')[2]); p(regex.exec('aa b')); }; btn0.onclick = output; btn1.onclick = () => pre0.textContent = ''; output();
0 コメント:
コメントを投稿