2017年4月12日水曜日

開発環境

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)の Boolean functions の Regular expressions (regexes) の Exercises on regexes.を JavaScript で取り組んでみる。

Regular expressions (regexes) の Exercises on regexes.

コード(Emacs)

HTML5

<button id="run0">run</button>
<button id="clear0">clear</button>
<pre id="output0"></pre>

<script src="sample_regexes.js"></script>

JavaScript

let input0 = document.querySelector('#word0'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0');

let output = () => {
    pre0.textContent += '1\n';
    let result = 'abcde1234567890abcde'.match(/[0-9]+/);
    pre0.textContent += result[0] + '\n';

    pre0.textContent += '2\n';
    result = 'abcde1234567890abcde'.match(/[0-7]+/);
    pre0.textContent += result[0] + '\n';

    pre0.textContent += '3\n';
    result = 'abcde1234567890abcde'.match(/\w+/);
    pre0.textContent += result[0] + '\n';

    pre0.textContent += '4\n';
    result = /\W(a\w+)/.exec('perl6 abcde python javascript');
    pre0.textContent += result[1] + '\n';

    pre0.textContent += '5\n';
    result = /\W([a-z]+\w*)/.exec('perl6 abcde python javascript');
    pre0.textContent += result[1] + '\n';

    pre0.textContent += '6\n';
    result = /\W([a-z]+\w*)/i.exec('perl6 Abcde python javascript');
    pre0.textContent += result[1] + '\n';

    pre0.textContent += '7\n';
    result = /([a-z])\1/i.exec('perl6 Abcde python javascript aabcde');
    pre0.textContent += result[0] + '\n';

    pre0.textContent += '8\n';
    result = 'perl6 aabcde Abcde python javascript abbcde'.match(/([a-z])\1/g);
    pre0.textContent += result[1] + '\n';
};
    

btn0.onclick = output;
btn1.onclick = () => {
    pre0.textContent = '';
};

output();













						

0 コメント:

コメントを投稿