replace()在尝试从字符串中删除HTML时不起作用

问题描述 投票:0回答:1

我正在运行fetch()以获取作业。我正在提取其中包含HTML标记的职位描述。例如:

我有(代码段,下面的完整代码):

var description = data.jobs[i].content;
var description = description.replace(/<(.|\n)*?>/g, '');

此打印件:

<p><span style="font-weight: 400;">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua...

就像替换功能甚至没有运行?

代码:

fetch('https://boards-api.greenhouse.io/v1/boards/cutover/jobs?content=true', {})

  .then(function (response) {
    return response.json(); // response type (json)
  })

  .then(function (data) {
    appendDataToHTML(data); // function that appends data to HTML
  })

  .catch(function (err) {
    console.log(err);
  });


  function appendDataToHTML(data) {
    var mainContainer = document.getElementById("jobListing");

    // get count of json objs
    var data_count = Object.keys(data.jobs).length;

    console.log(data_count);

    // for each object, create card
    for (var i = 0; i < data_count; i++) {

      var job_title     = data.jobs[i].title;
      var job_location  = data.jobs[i].location.name;
      var job_link      = data.jobs[i].absolute_url;
      var description   = data.jobs[i].content;

      var description = description.replace(/<(.|\n)*?>/g, '');
      var description = description.substring(0,200); 

      var html =
        '<div class="card">'+
          '<div class="card__body">'+
            '<div class="card__title">'+ job_title +'</div>'+
            '<div class="card__subtitle">'+ job_location +'</div>' +
            '<div class="card__copy">'+ description +'</div>'+
          '</div>'+
        '</div>';

      mainContainer.insertAdjacentHTML('beforeend', html);
    }

  }
<div id="jobListing"></div>
javascript html
1个回答
-1
投票

您有两个具有相同名称的变量。我建议您重命名第二个变量,看看是否可以解决。

© www.soinside.com 2019 - 2024. All rights reserved.