I receive text from response:
"Generate something in 1:1 format for Birthday
in an cheerful, celebratory
mood. The image should be created in an Graffiti
style. The text Happy Birthday!
is at the top. At the bottom is the lettering: Liam
. Graphic elements should visually reflect the terms stars, balloons
."
and have string without values ready:
const sub = "Generate something in 1:1 format for in an mood. The image should be created in an style. The text is at the top. At the bottom is the lettering:. Graphic elements should visually reflect the terms.";
In every response words can differ, for example Birthday->Thank You, Liam->John Doe, stars, balloons->fireworks and many other combinations.
Also, basic strings, prompt
and sub
can be in different languages.
Is there a way to extract marked words (which will be used to prefil some input fields)?
I managed to do that only if the words are quoted like:
const sub = "Generate something in 1:1 format for in an mood. The image should be created in an style. The text is at the top. At the bottom is the lettering:. Graphic elements should visually reflect the terms."
const prompt = "Generate something in 1:1 format for 'Birthday' in an 'cheerful, celebratory' mood. The image should be created in an 'Graffiti' style. The text 'Happy Birthday!' is at the top. At the bottom is the lettering: 'Liam'. Graphic elements should visually reflect the terms 'stars, balloons'."
const resolvePrompt = (prompt, sub) => {
return prompt
.replaceAll(".", "")
.split(" ")
.filter((wd) => !sub.includes(wd))
.join("")
.split("''");
};
console.log(resolvePrompt(prompt, sub))
2
Answers
You just need to use few regex, if you are new, you can ask AI to make it for you.
Here:
(.+?)
where.+
together matches one or more than one character in sequence until default pattern follows,?
makes matching less greedy,i.e it will match least possible until it gets back to track..match(regex)
return corresponding matches from which we extract content in orderIf my guess is right, I think the problem could be solved like this:
NOTE that it would be useful to add as many punctuations in the following pattern as possible:
IMPORTANT: the
parseVariableString
can use both an array or an object astheKeyValuePairs
parameter.