使用JavaScript从txt文件中提取以'E:'开头,以'R:'结尾并包含中文的特定内容。首先,需要将txt文件读取到js中。可以使用XMLHttpRequest对象或者fetch API来实现。

以XMLHttpRequest为例,代码如下:

const xhr = new XMLHttpRequest();
xhr.open('GET', 'file.txt', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    const content = xhr.responseText;
    // TODO: 在这里处理content
  }
};
xhr.send();

然后,我们需要从content中抽取出符合条件的部分。可以使用正则表达式来实现。

const regex = /E:[一-龥]+R:/g;
const matches = content.match(regex);

if (matches) {
  matches.forEach(match => {
    console.log(match);
  });
}

完整代码如下:

const xhr = new XMLHttpRequest();
xhr.open('GET', 'file.txt', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    const content = xhr.responseText;

    const regex = /E:[一-龥]+R:/g;
    const matches = content.match(regex);

    if (matches) {
      matches.forEach(match => {
        console.log(match);
      });
    }
  }
};
xhr.send();
JS提取txt文件特定内容:以E:开头,R:结尾,含中文

原文地址: https://www.cveoy.top/t/topic/mPdt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录