通过换行符和JavaScript中的制表符拆分文本文件 [英] Splitting text file by newlines and tab in JavaScript

查看:475
本文介绍了通过换行符和JavaScript中的制表符拆分文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载这样的文本文件...

I am loading a text file like this...

Cue         Target      Info      Group
CONTACTS    FRIENDS     genuine   Group1
EXECUTIVE   PRODUCER    genuine   Group1

通过具有此功能的jQuery ...

via jQuery with this function...

var stimuliArray = "";
jQuery.get("Words1.txt", function(data){
  stimuliArray = data.split("\n")
});

这样可以很好地将制表符分隔的文件按每行分隔开...

This works fine to split the tab-delimited file by each new line...

Cue Target  Info Group,CONTACTS FRIENDS genuine Group1,EXECUTIVE PRODUCER   genuine Group1

现在我也可以对其进行修改以按标签分割...

Now I can also modify it to split by tab...

var stimuliArray = "";
jQuery.get("Words1.txt", function(data){
  stimuliArray = data.split("\t")
});

它将给出此结果...

And it will give this result...

Cue,Target,Info,Group CONTACTS,FRIENDS,genuine,Group1 
EXECUTIVE,PRODUCER,genuine,Group1 

我希望输出像我的原始文件...

I want the output to be like my original file...

Cue         Target      Info      Group
CONTACTS    FRIENDS     genuine   Group1
EXECUTIVE   PRODUCER    genuine   Group1

...以便我可以像这样访问数据(例如array [2] [1]),输出将是....

...so that I can access data like this (e.g., array[2][1]) and the output would be....

PRODUCER

我试图在jQuery函数之后添加一行代码以按制表符或换行来拆分,但它根本不起作用...

I have tried to add a line of code to split by tab or new lines AFTER the jQuery function but it does not work at all...

stimuliArray = stimuliArray.split("\t");

使用此类代码,数组保持不变.

The array remains unchanged with that kind of code.

我只希望像普通数组一样可访问该文件,以便我可以逐行访问以及选择的任何索引访问信息.感谢您的提示!

I just want the file to be accessible like a normal array so that I can access information line-by-line and in whatever index I choose. Thanks for any hints!

推荐答案

您需要按行(\n)拆分文本,然后按制表符(\t)逐行拆分.这样的事情.

You need to split the text by lines (\n) and then each line by tab (\t). Something like this.

stimuliArray = data.split('\n').map(function(ln){
    return ln.split('\t');
});

这篇关于通过换行符和JavaScript中的制表符拆分文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆