javascript - 根据第一个数字分割文本 javascript

标签 javascript html

how to get numeric value from string?

从这里我了解如何从字符串中提取数字。但我还需要提取其后的信息,直到下一个数字。我有很多与此类似的文本,我需要提取每个时间戳。此文本是从 YouTube API 中提取的。

Information Technology- Lecture #1
June 4, 2015
Professor Vasarhelyi
Please visit our website at http://raw.rutgers.edu
Time Stamps:

00:00:28 What is ASEC?
00:02:59 Continuous Monitoring & Continuous Accounting
00:03:43 Assurance
00:07:25 Predictive v. Preventive (Traditional Audit)
00:10:36 Audit Data Standard (ADS)
00:16:37 XBRL and XML
00:20:13 How is technology changing our brains?
00:21:36 Singularity: Artificial Intelligence vs. Human Intelligence
00:37:57 Big Data
00:40:39 NSA Snooping
00:47:59 Internet Trends
00:59:58 E-Education: What will change?
01:08:42 What do you need to know in the age of Google?
01:13:45 Delivery, Assessment, and Granting
01:17:00 Automatic Student Learning Management System
01:20:49 A Degree’s Role in Society
01:23:02 Summary
01:28:52 Primary Priorities for Maintaining Relevance 
01:30:01 GAAP
Summary:
In this lecture, Professor Vasarhelyi introduces what the course will talk about in future sessions while reviewing key and basic concepts with the class.  He also discusses how the Internet changes the way that we think and whether or not robots will soon replace humans in the work force.
Please subscribe to our channel to get the latest updates on the RU Digital Library.

我当前的方法遇到了限制,因此我想知道是否可以使用其他方法来仅提取以下信息:

00:00:28 What is ASEC?
00:02:59 Continuous Monitoring & Continuous Accounting
00:03:43 Assurance
00:07:25 Predictive v. Preventive (Traditional Audit)
00:10:36 Audit Data Standard (ADS)
00:16:37 XBRL and XML
00:20:13 How is technology changing our brains?
00:21:36 Singularity: Artificial Intelligence vs. Human Intelligence
00:37:57 Big Data
00:40:39 NSA Snooping
00:47:59 Internet Trends
00:59:58 E-Education: What will change?
01:08:42 What do you need to know in the age of Google?
01:13:45 Delivery, Assessment, and Granting
01:17:00 Automatic Student Learning Management System
01:20:49 A Degree’s Role in Society
01:23:02 Summary
01:28:52 Primary Priorities for Maintaining Relevance 
01:30:01 GAAP

我还需要输入 <span>标签在前面,结束标签在每个时间戳的末尾。所以预期的输出:

<span>00:00:28 What is ASEC?</span>
<span>00:02:59 Continuous Monitoring & Continuous Accounting</span>
<span>00:03:43 Assurance</span>
<span>00:07:25 Predictive v. Preventive (Traditional Audit)</span>
<span>00:10:36 Audit Data Standard (ADS)</span>
<span>00:16:37 XBRL and XML</span>
<span>00:20:13 How is technology changing our brains?</span>
<span>00:21:36 Singularity: Artificial Intelligence vs. Human Intelligence</span>
<span>00:37:57 Big Data</span>
<span>00:40:39 NSA Snooping</span>
<span>00:47:59 Internet Trends</span>
<span>00:59:58 E-Education: What will change?</span>
<span>01:08:42 What do you need to know in the age of Google?</span>
<span>01:13:45 Delivery, Assessment, and Granting</span>
<span>01:17:00 Automatic Student Learning Management System</span>
<span>01:20:49 A Degree’s Role in Society</span>
<span>01:23:02 Summary</span>
<span>01:28:52 Primary Priorities for Maintaining Relevance</span>
<span>01:30:01 GAAP</span>

最佳答案

这是使用正则表达式和String.match的另一种方法。定义一个函数来从文本中提取时间戳行,并定义一个函数来输出它们。传递给第一个函数的正则表达式为:/\n\d.*(?=\n)/g,其中表示:查找以数字作为第一个字符的每个新行,并且接下来是全局的另一个换行符。请参阅下面的演示片段。

注意:如果您还可以在第二行获取日期(June 4, 2015),您甚至可以向对象添加 date 属性,并构造只需执行 result[i].date = new Date('June 4, 2015' + ' ' + result[i].time) 即可获取 Javascript 日期(可转换为 unicode 时间戳等)findTimestamps 函数中。

var text = document.getElementsByTagName('p')[0].textContent;

function findTimestamps(regex, target) {
  var result = target.match(regex);
  for (var i = 0; i < result.length; i++) {
    result[i] = { 
      time: result[i].slice(1, result[i].indexOf(' ')),
      msg: result[i].slice(result[i].indexOf(' ') + 1)
    };
  }
  return result;
}
function outputTimestamps(target, array) {
  var output = '';
  for (var i = 0; i < array.length; i++) {
    output += '<p><span>' + array[i].time + '</span>' + array[i].msg + '</p>';
  }
  target.innerHTML = output;
}

var r = findTimestamps(/\n\d.*(?=\n)/g, text);
outputTimestamps(document.getElementsByTagName('div')[0], r);
body>p { display: none; }
div:last-child { white-space: pre; }
span { margin-right: 20px; }
<p>Information Technology- Lecture #1
June 4, 2015
Professor Vasarhelyi
Please visit our website at http://raw.rutgers.edu
Time Stamps:
00:00:28 What is ASEC?
00:02:59 Continuous Monitoring & Continuous Accounting
00:03:43 Assurance
00:07:25 Predictive v. Preventive (Traditional Audit)
00:10:36 Audit Data Standard (ADS)
00:16:37 XBRL and XML
00:20:13 How is technology changing our brains?
00:21:36 Singularity: Artificial Intelligence vs. Human Intelligence
00:37:57 Big Data
00:40:39 NSA Snooping
00:47:59 Internet Trends
00:59:58 E-Education: What will change?
01:08:42 What do you need to know in the age of Google?
01:13:45 Delivery, Assessment, and Granting
01:17:00 Automatic Student Learning Management System
01:20:49 A Degree’s Role in Society
01:23:02 Summary
01:28:52 Primary Priorities for Maintaining Relevance 
01:30:01 GAAP
Summary:
In this lecture, Professor Vasarhelyi introduces what the course will talk about in future sessions while reviewing key and basic concepts with the class.  He also discusses how the Internet changes the way that we think and whether or not robots will soon replace humans in the work force.
Please subsc</p>
<div></div>
<div></div>

关于javascript - 根据第一个数字分割文本 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31572338/

相关文章:

javascript - vue中如何使用download属性?

javascript - 创建 protected react 路线

javascript - 从 cookie 重新加载 Google 地点数据

javascript - 如何让chrome/firefox能够运行parent.frame

javascript - JQuery 动画无法正常工作

html - 使元素固定位置,但如果内容太大则跟随页面滚动

javascript - ramda 中的多个数据的管道

javascript - Mapbox 搜索,打开弹出窗口/工具提示并更改自定义标记图像

javascript - 如何使用@angular/cli(angular-cli、ng cli)定义webpack入口文件?

jQuery onmouseover + onmouseout/悬停在两个不同的 div 上