0

I've successfully pulled in the JSON below however I am having an issue turning those pesky regex \r\n\r\n's into <br/>'s. I'm currently using the following code but it only escapes a few. I'm looking to replace ALL instances of regex including: \r\n, \r\n\r\n. The code I used before is:

$('<ul class="job-listing"><li class="job-position"><h2>'+post.m_positionName+'</h2></li><li class="job-description">'+post.m_description.replace('\r\n','<br />')+'</li></ul>').appendTo('body'); 

Here is the JSON:

[ { "m_id": 473644, "m_positionName": "Application Monitoring Software Engineer", "m_positionLocations": [ {} ], "m_active": true, "m_description": "Job Responsibilities:\r\n\r\n-Create world class application monitoring tools and dashboards for our health care applications\r\n\r\n-Develop business rules to pro actively identify and re-mediate system-level issues before they occur.\r\n\r\n-Create business intelligence reports for internal and external use as a supplement to software products.\r\n\r\n\r\n\r\nJob Requirements:\r\n\r\n-BS or MS Degree in computer science or any engineering discipline.\r\n-4+ years of experience with Java (or other object-oriented programming language).\r\n-Experience in SQL, Struts, Hibernate, Spring, Eclipse, JSP, JavaScript.\r\n-Highly motivated and self-driven personality.\r\n-Excellent interpersonal and leadership skills.\r\n-A vision for the future and a desire to make a difference.\r\n-Experience with Maven, Tomcat, PostgreSql, Jasper Reports,", "m_postedDate": "Jun 29, 2012 9:17:19 AM", "m_closingDate": "Jun 29, 2013 12:00:00 AM" } 
2
  • Just to be clear, does \r\n\r\n turn into <br /> or <br /><br />? Commented Nov 21, 2012 at 14:50
  • I'd have to see how it looks but lets say for now any instance of \r\n will turn into a single <br/> Commented Nov 21, 2012 at 14:51

2 Answers 2

6

Use this:

post.m_description.replace(/\r\n|\n|\r/g, '<br />'); 
Sign up to request clarification or add additional context in comments.

2 Comments

Where does that code go? Does it just replace what I wrote? post.m_description.replace('\r\n','<br />')+'</li></ul>')
@user1324700 Yes the replace there should be changed to the global regex that I have provided in my answer. I have edited the answer.
1

Try this:

replace(/\r\n/, '<br/>') 

regular expressions in JS don't need to be encapsulated by quotes

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.