js将txt文件中每行带有DTCode或是TxtID的的数据提取出来不要带有DTCode放到一个数组里
可以使用JavaScript的File API来读取txt文件,然后使用正则表达式匹配每一行的数据。以下是一个示例代码:
// 读取txt文件
function readTxtFile(file) {
const reader = new FileReader();
reader.onload = function(e) {
const contents = e.target.result;
// 将文件内容按行分割成数组
const lines = contents.split('\n');
// 匹配包含DTCode或TxtID的行,并提取数据
const regex = /(DTCode|TxtID):(.+)/;
const results = [];
lines.forEach(function(line) {
const match = line.match(regex);
if (match) {
results.push(match[2].trim());
}
});
console.log(results); // 输出包含DTCode或TxtID的数据数组
};
reader.readAsText(file);
}
// 使用示例
const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', function(e) {
const file = e.target.files[0];
readTxtFile(file);
});
你可以在HTML中添加一个文件选择框 <input type="file">,然后将其绑定到change事件上,当选择文件后,调用readTxtFile函数读取文件并提取数据。最后,将提取的数据输出到控制台或做其他处理。
原文地址: https://www.cveoy.top/t/topic/i9jY 著作权归作者所有。请勿转载和采集!