-1

i am currently using pre tag , in pre tag the text format is pretty raw. what i want to do is put something on end of each sentence in pre tag like the example below, how can i acheive this?

sample pre tag

<pre>
hello
good world
my name is
dan
</pre>

i want to add $ symbol at the end of each sentence like this

<pre>
hello $
good world $
my name is $
dan $
</pre>

how can i achieve something like this with the help of javascript and react js

2 Answers2

1

You could split() along line breaks and join() the way you want it:

for(let pre of document.getElementsByTagName("pre"))
  pre.innerHTML=pre.innerHTML.split("\n").join(" $\n");
<pre>
hello
good world
my name is
dan
</pre>
tevemadar
  • 12,389
  • 3
  • 21
  • 49
  • how do i do this in react js –  Jun 12 '21 at 15:20
  • for that one you should show how that `
    ` tag got there - because it should be done before that. as a developer of react writes [here](https://stackoverflow.com/a/23572967/7916438): *Broadly speaking, you shouldn't manipulate by hand the DOM that React has created.* - then she continues that technically you can do anything, but the "React way" would be rendering the final content, so the one which already has the symbols inside.
    – tevemadar Jun 12 '21 at 15:31
0

Perhaps a simple text replace would suffice

const pre = document.querySelector('pre');

pre.textContent = pre.textContent.replace(/\n/g,' $\n');
<pre>
hello
good world
my name is
dan
</pre>
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317