日々のコンピュータ情報の集積と整理

Dr.ウーパのコンピュータ備忘録

2015年2月18日水曜日

プログラムソース:コンマ(,)区切りの文字列の要素を、括弧[]でそれぞれ囲む - ver1.0

これは何?

以下のページで動作させているプログラムのソースコードです。

Dr.ウーパのコンピュータ備忘録: コンマ(,)区切りの文字列の要素を、括弧[]でそれぞれ囲む : Comma Separated Words to Square-Bracket Separated Words
http://upa-pc.blogspot.jp/p/double-quotes-comma-to-square-bracket.html


入力されたコンマ(,)区切りの文字列の要素を、括弧[]でそれぞれ囲んだ結果を出力します。

なお、このソースコードは初版(Ver1.0)であり、現在動作しているプログラムとは異なっている可能性があります。
→ 新しいバージョンがあります。

プログラムソース:コンマ(,)区切りの文字列の要素を、括弧[]でそれぞれ囲む - ver1.1:Blogger特有の問題の修正
http://upa-pc.blogspot.com/2015/02/Src-Comma-Separated-Words-to-Square-Bracket-Separated-v1.1.html



ソースコード:

<form onsubmit="return false;">
    <strong>(ダブルクォーテーション(")で囲まれた)コンマ(,)区切りの文字列 :</strong><br />
    <input type="text" id="input-text" style="width: 700px" /><br />
    <input type="checkbox" id="auto-focus-move" checked="checked" />自動的に結果を選択する<br />
    <p>
    例)“Dr.ウーパ , コンピュータ備忘録 , Blog”
    </p>
</form>

<form onsubmit="return false;">
    <strong>括弧[]でそれぞれの要素を囲んだ結果:</strong><br />
    <input onfocus="this.select();" type="text" id="output-text" style="width: 700px" />
</form>
<br />


<script type="text/javascript">
<!--
    (function () {

        var id_input = "input-text";        // 入力値を保持する要素の id
        var id_output = "output-text";      // 出力値を保持する要素の id

        // 前回検査時の入力値
        var input_old_text = "";

        // 定期的に入力を監視し、入力が変化していたら、出力する
        setInterval((function () {

            // 入力されたダブルクォーテーション(")で囲まれたコンマ(,)区切りの文字列
            var input_text = document.getElementById(id_input).value;

            if (input_text != input_old_text) {

                // 入力されたダブルクォーテーション(")で囲まれたコンマ(,)区切りの文字列を、
                // 括弧[](角括弧、大括弧、ブラケット)でそれぞれ囲んだ文字列に変換する
                var output_text = changeInputText(input_text);

                document.getElementById(id_output).value = output_text;
                if (document.getElementById("auto-focus-move").checked) {
                    document.getElementById(id_output).select();
                }

                input_old_text = input_text;

            }

        }), 500);

        /*
        入力されたダブルクォーテーション(")で囲まれたコンマ(,)区切りの文字列を、
        括弧[](角括弧、大括弧、ブラケット)でそれぞれ囲んだ結果を返す
        */
        function changeInputText(input_text) {

            return "[" +
                input_text.replace(/["“”]/g, "")
                    .replace(/\s*[,,]\s*/g, "][") +
                "]";
        }


        var debug_flag = false;
        if (debug_flag) {

            /* テスト用 */
            (function () {

                var error_count = 0;

                (function () {
                    var input = "“Dr.ウーパ,コンピュータ備忘録 , Blog”";
                    var output = changeInputText(input);
                    var error = output != "[Dr.ウーパ][コンピュータ備忘録][Blog]";
                    if (error) error_count++;
                    console.log("input," + input + ",output," + output + ",error," + error);
                })();
                (function () {
                    var input = "\"Dr.ウーパ , コンピュータ備忘録,Blog\"";
                    var output = changeInputText(input);
                    var error = output != "[Dr.ウーパ][コンピュータ備忘録][Blog]";
                    if (error) error_count++;
                    console.log("input," + input + ",output," + output + ",error," + error);
                })();
                (function () {
                    var input = "Dr ウーパ コンピュータ 備忘録 , Bl og";
                    var output = changeInputText(input);
                    var error = output != "[Dr ウーパ コンピュータ 備忘録][Bl og]";
                    if (error) error_count++;
                    console.log("input," + input + ",output," + output + ",error," + error);
                })();

                console.log("error_count," + error_count);
                if (error_count > 0) {
                    console.log("Test NG");
                } else {
                    console.log("Test OK");
                }
            })();
        }
    })();

//-->
</script>


解説

プログラムのソースコードについての解説は、次のページで行っています。

プログラムソース解説:コンマ(,)区切りの文字列の要素を、括弧[]でそれぞれ囲む - ver1.0
http://upa-pc.blogspot.com/2015/02/Explain-Comma-Separated-Words-to-Square-Bracket-Separated-v1.0.html







関連記事

関連記事を読み込み中...

同じラベルの記事を読み込み中...