gpt4 book ai didi

javascript - 在js中用多个定界符和特殊字符分割

转载 作者:行者123 更新时间:2023-11-30 06:13:38 26 4
gpt4 key购买 nike

我正在尝试为基于各种字符串和运算符的拆分字符串创建一个正则表达式。我们如何做到这一点?

下面是我的代码:

var author;
var authorResult = [];
ByREGEX=/By|From|says\s|,/g;
author = authorByline.split(ByREGEX);
if(!author[1].trim()) {
author[1] = author[2].trim();
author[2] = '';
}
authorResult['name'] = author[1].trim();

if("2" in author){
authorResult['role'] = author[2].trim();
} else {
authorResult['role'] = '';
}

return authorResult;

下面是我的字符串和预期输出:

From Bru Water(Delimeter:From) : Expected output(Author: Bru Water, Role:'')

By Matth Moo, Med Corresponde(Delimeter:'By' , ',') : **Expected output(Author: Matth Moo, Role:Med Corresponde)**

Analysis by Davidd Cross in London(Delimeter:'Analysis by' , 'in') : **Expected output(Author: Davidd Cross, Role:'')**

left and right, says Daavid Aaronovi(Delimeter:'says'): **Expected output(Author: Daavid Aaronovi, Role:'')**

From Dav Chart and Bo De(Delimeter:'From','and') : **Expected output(Author1: Dav Chart, Role1:'',Author2: Bo De, Role2:'')**

By Oliv Wrig, Poli Edit, and Franc Ellio, Politic Edit(Delimeter:'By','and'): **Expected output(Author1: Oliv Wrig, Role1:'Poli Edit',Author2: Franc Ellio, Role2:'Politic Edit')**

By RCAik Brbent(Delimeter:'By'): Expected output(Author: RCAik Brbent, Role:'')

From TomTY Knowl, Technolog Reporte(Delimeter:'From',','): **Expected output(Author: TomTY Knowl, Role:'Technolog Reporte')**

最佳答案

我设法使用拆分和连接做了一些非常大的事情

还有一些问题,比如 Davidd Cross in London

它返回一个数组而不是一个对象

如果您需要我清理更多数据,请在评论中告诉我,但我认为您应该可以自己完成


使用数组设置作者、 Angular 色和其他作者之间的标识符和分隔符,并针对字符串运行它们

let lines = [
"From Bru Water", // : Expected output(Author: Bru Water, Role:'')
"By Matth Moo, Med Corresponde", // : **Expected output(Author: Matth Moo, Role:Med Corresponde)**
"Analysis by Davidd Cross in London", // : **Expected output(Author: Davidd Cross, Role:'')**
"left and right, says Daavid Aaronovi", // : **Expected output(Author: Daavid Aaronovi, Role:'')**
"From Dav Chart and Bo De", // : **Expected output(Author1: Dav Chart, Role1:'',Author2: Bo De, Role2:'')**
"By Oliv Wrig, Poli Edit, and Franc Ellio, Politic Edit", //: **Expected output(Author1: Oliv Wrig, Role1:'Poli Edit',Author2: Franc Ellio, Role2:'Politic Edit')**
"By RCAik Brbent", // : Expected output(Author: RCAik Brbent, Role:'')
"From TomTY Knowl, Technolog Reporte" // : **Expected output(Author: TomTY Knowl, Role:'Technolog Reporte')**
]

let nameIdentifier = ["from", "says", "by"] // these are followed by an Author name
let authorsSeparator = ["and"] // these are between two Authors
let authorRoleSeparator = [","] // these are between an Author and it's role
let tempSeparator = "somethingWhichAppearNowhereElse"

let result = lines.map(line => {
// get authors
let authors = line
authorsSeparator.forEach(separator => {
authors = line.split(separator).join(tempSeparator)
})
authors = authors.split(tempSeparator)


// remove first object of array if not an authors
let keep = false
nameIdentifier.forEach(identifier => {
keep |= authors[0].toLowerCase().includes(identifier)
})
if(! keep) { authors.shift() } // remove the first entry from the array

// remove the identifiers to get the authors name
authors.forEach((auth, i) => {
nameIdentifier.forEach(identifier => {
let identifierIndex = auth.toLowerCase().indexOf(identifier)
if(identifierIndex !== -1) {
auth = auth.substring(identifierIndex + identifier.length)
}
authors[i] = auth.trim()
})
})

// separator authors name from their roles
return authors.map(auth => {
let author = auth
authorRoleSeparator.forEach(separator => {
author = auth.split(separator).join(tempSeparator)
})
return author.split(tempSeparator)
})
})

console.log(result)

关于javascript - 在js中用多个定界符和特殊字符分割,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57394879/

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