正则表达式,并将字符串转换为数组和来回Javascript

正则表达式,并将字符串转换为数组和来回Javascript,javascript,regex,arrays,string,Javascript,Regex,Arrays,String,我从一个文本文件中读取数据,我对一个特定的模式感兴趣,该模式与以下内容隔离: cleanString = queryString.match(/^NL.*/gm); 这将导致阵列: ["NL:What are the capitals of the states that border the most populated states?", "NL:What are the capitals of states bordering New York?", "NL:Show the stat

我从一个文本文件中读取数据,我对一个特定的模式感兴趣,该模式与以下内容隔离:

 cleanString = queryString.match(/^NL.*/gm);
这将导致阵列:

["NL:What are the capitals of the states that border the most populated states?",
"NL:What are the capitals of states bordering New York?",
"NL:Show the state capitals and populations.", 
"NL:Show the average of state populations.", 
"NL:Show all platforms of Salute generated from NAIs with no go mobility."]
然后我想去掉所有匹配NL的模式:所以我只剩下一个自然语言问题或语句。为此,我将数组转换为字符串,然后使用.split()创建所需的数组,如下所示:

var nlString = cleanString.toString();
var finalArray = nlString.split(/NL:/gm);
我有两个问题。 1.我在结果数组的索引[0]处得到一个额外的空字符串值,然后 2.现在,我将逗号文字附加到数组中的字符串:

["", "What are the capitals of the states that border the most populated states?,",
"What are the capitals of states bordering New York?,",
"Show the state capitals and populations.,", 
"Show the average of state populations.,", 
"Show all platforms of Salute generated from NAIs with no go mobility."]
如何消除这些问题?此外,如果有人有一种更优雅的方法来读取一个由换行符分隔的丑陋的大文本文件,并隔离感兴趣的字符串,我会全神贯注


提前感谢您的建议

您不必将数组转换为字符串,然后删除
NL:
字符串并转换回数组,只需迭代数组并在每个索引中删除该字符串即可

var arr = arr.map(function(el) {return el.replace('NL:','');});


如果旧浏览器出现问题,常规for循环也会起作用。警告:
map
不受IE8及以下版本的支持。这里有一个替代方案:

var finalString = nlString.replace(/NL:/gm, '');
var array = string.split(/\n*NL:/);
array.shift(); // that's it
此处演示: