OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Escaping, unicode, character escapes while parsing/logging string in javascript

  • Thread starter Thread starter Arnav Kumar
  • Start date Start date
A

Arnav Kumar

Guest
i got a string, which is from user input, i store it in a variable say, str

Code:
let str = "some user generated better string that contains unicode escapes \u1451 \u3516 with some otehr escapes \n \t \r \b \\ / and so on";

i need to print the text as is, so that it exactly matches the user provided string, i am able to achive parsing of \n \t etc using replace, the problem causing ones are \b trying to replace is also replaces the surroundings of words with \\b and i am even unable to match for \u to replace to to escape, it gets converted to its respective character, the moment its stored as string

i got to know about String.raw, but that only seems to work with template literals, using

Code:
String.raw`${str}`

does escape others, but again those unicode characted gets converted to their respective char, and unable to get escaped.

<p>i got a string, which is from user input, i store it in a variable say, <code>str</code></p>
<pre class="lang-js prettyprint-override"><code>let str = "some user generated better string that contains unicode escapes \u1451 \u3516 with some otehr escapes \n \t \r \b \\ / and so on";
</code></pre>
<p>i need to print the text as is, so that it exactly matches the user provided string,
i am able to achive parsing of <code>\n</code> <code>\t</code> etc using replace, the problem causing ones are <code>\b</code> trying to replace is also replaces the surroundings of words with <code>\\b</code>
and i am even unable to match for <code>\u</code> to replace to to escape, it gets converted to its respective character, the moment its stored as string</p>
<p>i got to know about <code>String.raw</code>, but that only seems to work with template literals, using</p>
<pre class="lang-js prettyprint-override"><code>String.raw`${str}`
</code></pre>
<p>does escape others, but again those unicode characted gets converted to their respective char, and unable to get escaped.</p>
 
Top