I am using Javascript to manipulate items in ZOTERO.
I have multiple items whose title contains " : " (a colon between spaces).
I want to replace this with ", " (a comma and a space).
For example:
This is : an example
should be
This is, an example
Can anyone tell me if the .replace statement in my code is correct?
var fieldName = "title";
var fieldID = Zotero.ItemFields.getID(fieldName);
var s = new Zotero.Search();
s.libraryID = ZoteroPane.getSelectedLibraryID();
s.addCondition(fieldName, 'contains', ' ');
var ids = await s.search();
if (!ids.length) {
return "No items found";
}
await Zotero.DB.executeTransaction(async function () {
for (let id of ids) {
let item = await Zotero.Items.getAsync(id);
let mappedFieldID = Zotero.ItemFields.getFieldIDFromTypeAndBase(item.itemTypeID, fieldName);
let fieldID = mappedFieldID || fieldID;
item.setField(fieldID, item.getField(fieldID).replace(/[\s:\s]/g, ', '));
await item.save({
skipDateModifiedUpdate: true
});
}
});
return ids.length + " item(s) updated";
[\s:\s]is whitespace or:. You'd want\s[:]\sor probably more likely space:space (SO won't format spaces in code blocks correctly).\sis any white space, not just a space.