0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AICLIの出力につく「行番号」を一括削除するHTMLツール

0
Posted at

本文

AICLI等のツールが出力したコードをコピペする際、先頭の行番号が邪魔になる問題を解決するため、ブラウザだけで動くシンプルなツールを作りました。

👇コピペ用の全体コードはこちら(クリックで開きます)

:point_left:ソースコードを表示する(クリックで開きます)
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>行番号削除ツール</title>
    <style>
        body {
            font-family: sans-serif;
            padding: 20px;
            max-width: 800px;
            margin: auto;
            background-color: #f9f9f9;
        }
        h2 {
            color: #333;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }
        textarea {
            width: 100%;
            height: 250px;
            padding: 10px;
            font-family: monospace;
            font-size: 14px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
            resize: vertical;
        }
        .controls {
            display: flex;
            gap: 10px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            color: white;
            font-weight: bold;
        }
        #btn-convert { background-color: #007bff; }
        #btn-convert:hover { background-color: #0056b3; }
        #btn-copy { background-color: #28a745; }
        #btn-copy:hover { background-color: #218838; }
    </style>
</head>
<body>

    <h2>行番号削除ツール</h2>
    <p>コピーしたテキストを上のボックスに貼り付けて、「行番号を削除」をクリックしてください。</p>

    <div class="container">
        <textarea id="inputText" placeholder="ここにテキストを貼り付けてください...
  1  [*] --> 初期表示状態
  2  
  3  state 初期表示状態 { ..."></textarea>
        
        <div class="controls">
            <button id="btn-convert" onclick="removeLineNumbers()">行番号を削除 ↓</button>
            <button id="btn-copy" onclick="copyToClipboard()">結果をコピー</button>
        </div>

        <textarea id="outputText" placeholder="結果がここに表示されます..." readonly></textarea>
    </div>

    <script>
        function removeLineNumbers() {
            const inputText = document.getElementById('inputText').value;
            
            // 行ごとに分割し、行頭の「空白(0個以上) + 数字(1個以上) + 空白(0個以上)」を削除
            const resultText = inputText.split('\n').map(line => {
                // 正規表現: ^\s*\d+\s*
                // ^   : 行頭
                // \s* : 空白が0個以上
                // \d+ : 数字が1個以上
                // \s* : 空白が0個以上
                return line.replace(/^\s*\d+\s*/, '');
            }).join('\n');
            
            document.getElementById('outputText').value = resultText;
        }

        function copyToClipboard() {
            const outputText = document.getElementById('outputText');
            if (!outputText.value) return; // 空の場合は何もしない

            // クリップボード API を使用
            navigator.clipboard.writeText(outputText.value).then(() => {
                alert('クリップボードにコピーしました!');
            }).catch(err => {
                // 古いブラウザ向けのフォールバック
                outputText.select();
                document.execCommand('copy');
                alert('クリップボードにコピーしました!');
            });
        }
    </script>

</body>
</html> 

仕組み

JavaScriptの replace(/^\s*\d+\s*/, '') を使い、行頭にある「数字」と「その前後の空白」だけを正規表現で削除しています。コードの本来のインデントは崩れません。

手元に置いておくと地味に便利なので、ぜひ使ってみてください!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?