gpt4 book ai didi

javascript - 如何处理文本以创建带有一些子字符串的新数组?

转载 作者:行者123 更新时间:2023-12-03 06:49:30 24 4
gpt4 key购买 nike

我正在 html 上编写一个脚本,我的想法是我想处理如下所示的文本:

"TW|223SDSDr33|Archive" "Yes"
"TW|ASFFSDFSFASDFS|Name" "LOCALggr"
"TW|AFFSFSFSDFSFASDFS|AFFAckAssocCd" ""
"TW|12AFFFSDFASFSFASDFS|AFFAckCommID" "fsdf"
"TW|FSFASFFSDFSFASDFS|AFFAckLevel" "fsdf Supported"
"TW|AFFSDFAASFSA|AFFAckRqst" "No Requedfst"
"TW|AFFSDFSFASDFS|AFFAckTestInd" "Test"
"TW|sfasfsSFSAFAS|AFFAckVersion" "fsdfs"

我想处理文本以创建一个名为“words”的数组,其中包含使用管道作为分隔符的先前文本的子字符串,如下所示:

words=["TW,223SDSDr33,Archive" "Yes",...,"TW,sfasfsSFSAFAS,AFFAckVersion" "fsdfs"]

为了实现这一目标,我尝试了:

var stringArray = document.getElementById("texto").value.split('\n');
document.write(stringArray.toString());
var arrayLength = stringArray.length;
for (var i = 0; i < arrayLength; i++) {
//Process every line;
}

这保存了我的文本区域然后对其进行处理,但问题是我不知道如何处理每一行以提取我想要的子字符串,为了更清楚这是完整的代码,我想感谢任何实现这一目标的建议,无论如何谢谢:

<!DOCTYPE html>
<html>

<body>
<p id="demo"></p>

<textarea cols=150 rows=10 id="texto">
"TW|223SDSDr33|Archive" "Yes"
"TW|ASFFSDFSFASDFS|Name" "LOCALggr"
"TW|AFFSFSFSDFSFASDFS|AFFAckAssocCd" ""
"TW|12AFFFSDFASFSFASDFS|AFFAckCommID" "fsdf"
"TW|FSFASFFSDFSFASDFS|AFFAckLevel" "fsdf Supported"
"TW|AFFSDFAASFSA|AFFAckRqst" "No Requedfst"
"TW|AFFSDFSFASDFS|AFFAckTestInd" "Test"
"TW|sfasfsSFSAFAS|AFFAckVersion" "fsdfs"

</textarea>

<script>
var words = [];
var stringArray = document.getElementById("texto").value.split('\n');
document.write(stringArray.toString());
var arrayLength = stringArray.length;
for (var i = 0; i < arrayLength; i++) {
//Do something;
}




</script>


</body>
</html>

最佳答案

我使用了正则表达式和Array.prototype.split。您可以根据您想要放入单词中的内容取消注释。

for (var i = 0; i < arrayLength; i++) {
var line = stringArray[i];
var quotes = /"(.*?)" "(.*?)"/.exec(line);
if (quotes) {
var first = quotes[1];
var last = quotes[2];

var separated = first.split("|");

// If you want to put the array of words
words.push(separated);

// In case you want them joined with a colon
// words.push(separated.join(","));

// If you want to add the second word that was in double quotes
// words.push(last);
}
}

// Uncomment to see results
// console.log(words);

关于javascript - 如何处理文本以创建带有一些子字符串的新数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37574210/

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