0

Replace space/↵ with next line in javascript

I am integrating backend API with Front end, populating field where each type should go to next line. But it's coming in same line

This is how it looks in UI console when I printed the field console.log(type);

type: "DNS↵DNS↵IPADDRESS↵DNS↵IPADDRESS↵NETWORK↵IPADDRESS↵IPADDRESS↵IPADDRESS↵IPADDRESS↵IPADDRESS↵IPADDRESS↵IPADDRESS↵IPADDRESS↵DNS↵IPADDRESS↵DNS↵IPADDRESS↵"

UI screen it looks like below

enter image description here Type : DNS DNS IPADDRESS DNS IPADDRESS NETWORK IPADDRESS IPADDRESS IPADDRESS IPADDRESS IPADDRESS IPADDRESS IPADDRESS IPADDRESS DNS IPADDRESS DNS IPADDRESS

Result should be like :

DNS                                                                                                                                                    
DNS 
IPADDRESS 
DNS 
IPADDRESS 
NETWORK 
IPADDRESS 
IPADDRESS 
IPADDRESS

I have tried

return type.replace(/↵/g, '\n');
return type.replace(/\s/g, '\n');
return type.replace(/" "/g, '\n');

return type.split("").join("\n");
return type.split("↵").join('\n');
return type.split("\n").join('\n');
userrj_vj_051620
  • 147
  • 2
  • 12
  • `console.log(escape(type));` what is the actual new line... NEXT how are you actually outputting it to the UI. My guess, that is the actual problem and not the new line. New lines and whitespace is meaningless in HTML for most elements. – epascarello Apr 14 '21 at 17:55
  • many html elements [treat whitespace differently](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Whitespace), e.g. ignoring newlines etc. Maybe you are missing a css `white-space: pre`? – ASDFGerte Apr 14 '21 at 17:59
  • Both `type.replace(/↵/g, '\n')` and `type.split("↵").join('\n')` will work for the given string (if it actually has those characters). If you are rendering a multi-line string to HTML, you need to set the white-space appropriately or split each line and render it as a separate text node, element, or add line-breaks (`
    `).
    – Mr. Polywhirl Apr 14 '21 at 18:03
  • @Mr.Polywhirl Can you help me with example? – userrj_vj_051620 Apr 14 '21 at 18:10
  • thank you all white-space: pre helped me to fix the issue. – userrj_vj_051620 Apr 14 '21 at 18:26

0 Answers0