// midjourney prompt 格式校验
function isValidString(inputString) {
const index = inputString.indexOf("--");
if (index == -1) {
return true;
}
inputString = inputString.substring(index);
const regex = /--[A-Za-z]+ [A-Za-z0-9:.]+/g;
const matches = inputString.match(regex);
if (!matches) {
return false;
}
// 拼接匹配到的子串,然后与原字符串比较
const reconstructed = matches.join(' ');
return reconstructed === inputString;
}
// midjourney prompt 预处理
function replacePunctuation(input) {
const keywords = [
"Aspect",
"ar",
"chaos",
"fast",
"iw",
"no",
"quality",
"q",
"relax",
"repeat",
"r",
"seed",
"stop",
"style",
"stylize",
"s",
"tile",
"turbo",
"weird",
"w",
];
let output = input.replace(
new RegExp(`--(?!${keywords.join("|")})`, "g"),
","
);
// 匹配除英文逗号和空格以外的标点符号,排除任意语种的文字
const regExp = /(?![\s.,:-])\p{P}/gu;
// 使用replace方法替换匹配到的标点符号为英文逗号
output = output.replace(regExp, ",");
output = output.trim();
return output;
}
const finalPrompt = replacePunctuation("An exquisite rose, with velvety petals in shades of deep red, unfurling gracefully amidst a bed of lush green leaves. Each petal is adorned with delicate dewdrops, glistening in the morning sunlight. The flower emits a captivating fragrance, filling the air with its sweet scent. A gentle breeze dances through the garden, causing the petals to sway and come to life. --ar 1:1");
console.log(finalPrompt);
const isValid = isValidString(finalPrompt);
console.log(isValid);
console