gpt4 book ai didi

javascript - 使用 javascript 和 regex 消除数组中的换行符

转载 作者:行者123 更新时间:2023-12-03 11:47:56 24 4
gpt4 key购买 nike

我有一个如下所示的文件:

QUERY:1
DATABASE:geoquery
NL:What are the capitals of the states that border the most populated states?
SQL:something

QUERY:2
DATABASE:geoquery
NL:What are the capitals of the states bordering New York?
SQL:SELECT state.Capital FROM state JOIN borderinfo ON state.State_Name = borderinfo.State_Name
WHERE borderinfo.Border = 'New York'

QUERY:3
DATABASE:geoquery
NL:Show the state capitals and populations.
SQL:SELECT state.Capital, state.Population FROM state

etc...

生成此文件的人拒绝以可用的格式(例如 XML 或 JSON)将其提供给我。所以我用 REGEX 解析它几次以获得我想要的结果。

剥离数据库以填充选择列表(工作正常):

$.get('inputQueryExamples.txt',
function(data){
var string = data;
var dbDynamo ='';
dbExp = new RegExp('(DATABASE:.*)','gm');
dbDynamo = string.match(dbExp);
cleanBreaks = new RegExp('(\r?\n|\r)','gm');
stringNow = dbDynamo.toString();
//console.log(dbDynamo);
dbDynamo = dbDynamo.map(function(el){return el.replace('DATABASE:','');});

var outArray = [];
for(i=0; i < dbDynamo.length; i++){
if ($.inArray(dbDynamo[i],outArray)== -1){
outArray.push(dbDynamo[i]);
}
}

dbDynamo = outArray.sort();

var options = '';
for(i=0; i<dbDynamo.length; i++){
options += '<option value="' + dbDynamo[i] + '">' + dbDynamo[i] + '</option>';
};
$(select).append(options);


});

当我第二次解析以获取与特定数据库关联的所有字符串时,问题就出现了。我最终在每个字符串前面都有一个换行符,这样当我用自动完成文本填充文本区域时,它就会从第二行开始:

Array [ "

NL:Show company with complaint against Debt collection product.,DATABASE:consumer", "

NL:Show issues and dates of complaints about HSBC companies.,DATABASE:consumer", "

NL:Show companies, issues and dates with consumer concerns.,DATABASE:consumer", "

NL:Show issues and companies with complaints in MA state." ]

$(document).ready(function(){
$.get('inputQueryExamples.txt',function(data){
var queryString = data;
var cleanString = "";
var db = '';
$('#database-list').change(function(){
db = $('#database-list').val();

// /(^DATABASE:.*\r\n)(^NL.*)/gm
// http://regex101.com/r/mN4hS2

regex = new RegExp('(^DATABASE:'+ db +'\r\n)(^NL.*)' ,'gm');

cleanString = queryString.match(regex);
//console.log(cleanString);
//cleanBreaks = new RegExp('(\r\n|\r|\n)(^NL.*)','gm');
//stringNow = cleanString.toString();
//var dirtyString
//dirtyString = stringNow.match(cleanBreaks);
//console.log(dirtyString);

var nlString = cleanString.map(function(el) {return el.replace('DATABASE:' + db,'');});
nlString = nlString.map(function(el){return el.replace('NL:',''); });
//nlString = nlString.map(function(el){return el.replace('\\r','').replace('\\n','');});

console.log(nlString.pop());
$('#query-list').autocomplete({
source:nlString,

});

}); // end change

我已经尝试了所有我能想到的方法来消除换行符,但没有成功。任何想法,将不胜感激。不幸的是,服务器端仅以可用格式提供数据的方式是不可行的。其中有很多额外的代码只是为了让您了解我所尝试的内容。无用的东西我都注释掉了。提前致谢。

最佳答案

首先使用 JavaScript 将给定的数据结构解析为 JavaScript 对象是有意义的,然后您可以更轻松地处理数据。

Here is a jsfiddle演示如何将数据解析为 JavaScript 对象。下面是进行解析的相关代码:

var datasources = {};

var parseData = function(block) {
var db = block.match(/DATABASE:(.*)/)[1];
var dbdata = {id: 0, nl: "", sql: ""};

if (!datasources[db]) {
datasources[db] = [];
}

dbdata.id = block.match(/QUERY:(.*)/)[1];
dbdata.nl = block.match(/NL:(.*)/)[1];
dbdata.sql = block.match(/SQL:(.*)/)[1];

datasources[db].push(dbdata);
};

var parseBlocks = function(data) {
var result = data.split('\n\n');
for(var i=0; i < result.length; i++) {
parseData(result[i]);
};
};

关于javascript - 使用 javascript 和 regex 消除数组中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com