用\ s替换\ r \ n's

时间:2012-11-21 14:46:00

标签: jquery regex json

我已成功提取下面的JSON但是我有一个问题将那些讨厌的正则表达式\ r \ n \ r \ n转换为<br/>。我目前正在使用以下代码,但它只是逃避了一些。我正在寻找替换正则表达式的所有实例,包括:\ r \ n,\ r \ n \ r \ n。我之前使用的代码是:

$('<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');

这是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 个答案:

答案 0 :(得分:6)

使用此:

post.m_description.replace(/\r\n|\n|\r/g, '<br />');

答案 1 :(得分:1)

试试这个:

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

正则表达式不需要用引号

封装