nikhil sukumar
nikhil sukumar

Reputation: 51

HTML escapes string with Backslashes

I am new to Javascript coding. The current task I am working on is creating HTML Documents from a text document.In a particular case, I have a complex string (alphanumeric string with backslashes). I need the HTML to print it the way the text is present. However, the HTML created escapes all backslashes.

Input Text:

3583859858.8200040004xxxx\00\0200040004\t0\t194.3\t39874\t7975\t90\t62\t0.1\t194.3\t200\t35.00\t35.00\t35.00\t194\t2.1\t12541\t12650\t13468\t13421\t13481\t13932\t35.00\t35.00\n

Output in HTML:

3583859858.8200040004xxxx0040004 0t194.3 39874 7975 90 62 0.1 194.3 200 35.00 35.00 35.00t194 2.1   12541 12650 13468 13421 13481 13932 35.00 35.00 

I tried below solutions to other relevant questions but it did not work

How to escape backslash in JavaScript?

Backslashes - Regular Expression - Javascript

How can I replace a backslash with a double backslash using RegExp?

Request help from seniors here

Upvotes: 0

Views: 1646

Answers (2)

Sachin Sanchaniya
Sachin Sanchaniya

Reputation: 1044

One Solution is to use JAVASCRIPT / JQUERY

Checkout This Demo

var string = '3583859858.8200040004xxxx\00\0200040004\t0\t194.3\t39874\t7975\t90\t62\t0.1\t194.3\t200\t35.00\t35.00\t35.00\t194\t2.1\t12541\t12650\t13468\t13421\t13481\t13932\t35.00\t35.00\n';

$("#result").html(string);

// IF THIS IS NOT WORKING THEN TRY THIS 

var htmlString = string.split("\\");
$("#resultHtml").html(htmlString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>
<br>
<div id="resultHtml"></div>

<!-- Third Solution ** &#160; FOR \t AND &#10; FOR \n-->
<div>3583859858.8200040004xxxx\00\0200040004&#160;0&#160;194.3&#160;39874&#160;7975&#160;90&#160;62&#160;0.1&#160;194.3&#160;200&#160;35.00&#160;35.00&#160;35.00&#160;194&#160;2.1&#160;12541&#160;12650&#160;13468&#160;13421&#160;13481&#160;13932&#160;35.00&#160;35.00&#10;</div>

This Might Be Helpful.

Upvotes: 1

Roshana Pitigala
Roshana Pitigala

Reputation: 8786

Use html character instead. Replace all backslashes with

&#92;

Example

<span>&#92;t0&#92;t194.3&#92;t39874</span>

Upvotes: 0

Related Questions