Remove Space and New Line from String and join using Comma

Introduction

Recently my clients asked me to set up a webpart which will trim the space and newline and will join the string using comma's. Business users use spreadsheet to observe datas and sometimes they need search functionalities too. So if they want to search the ID's then they need to copy multiple columns cells and put it in the search textbox to search. So the Search functionality should be built using comma separated values.

Description

In order to achieve this I went for the html along with the javascript for the validations. Below is my code which has a convert button to trim the space and the new line strings with comma separated.

<html>

<head>

    <script>

        function convert() {

            var data = document.getElementById("get").value;

            var lines = data.split('\r\n');

            for (var i = 0; i < lines.length; i++) {

                lines[i] = lines[i].replace(/^\s+|\s+$/g, '');

            }

            document.getElementById("display").value = lines.join(',');

        }

    </script>

</head>

<body>

    <div>

        Enter Code: </br>

        <textarea id="get"> </textarea>

        </br>

        <input type="button" value='Convert' onclick="convert()" />

        </br>

        <textarea id="display"> </textarea>

    </div>

</body>

</html>