首先,需要将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:[\u4e00-\u9fa5]+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:[\u4e00-\u9fa5]+R:/g;
    const matches = content.match(regex);

    if (matches) {
      matches.forEach(match => {
        console.log(match);
      });
    }
  }
};
xhr.send();
如何使用js抽取出txt文件中以E开头以R结尾的部分含中文给出完整代码

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

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