linmu
linmu

Reputation: 45

Is there a common function to translate string to special character?

Assuming that there is an input field named fieldDelimiter, and user input "\\t", i should translate '\\t' to '\t'. ('\\n' to '\n', '\\u0001' to '\u0001' .etc). Is there a common function to do this?

Upvotes: 0

Views: 51

Answers (1)

Lim Wen Rong
Lim Wen Rong

Reputation: 51

You can use JavaScript's replace() function to replace \\ to \

https://www.w3schools.com/jsref/jsref_replace.asp

To escape \, you can refer the code below:

<html>
    <body>
        <input type="text" id="inputValue">
        <button onclick="removeSlash()">Remove</button>
        <div id="result">
        </div>
    </body>
    <script>
        function removeSlash(){
            var inputValue = document.getElementById("inputValue");
            var result = document.getElementById("result");
            var removed = inputValue.value.replace("\\\\", "\\");
            result.innerHTML = removed;
        }
    </script>
</html>

Upvotes: 1

Related Questions