I have “fill in the blanks” embedded in a paragraph of text that I want to extract out and eventually replace.
For example:
“Hello [a], you rock! Do you like [y]? What about [z]?”
I want to extract the list of things that are within the brackets automatically. In this case, I want to get [a, y, z] so that I can display it in a table or something for people to edit (eg for [insert niche] to automatically populate under “Mapping Label” in the table below).
You can extract them with js using regex.
Based on your example:
const text = variables["..."]; // the value of the text
const match = text.matchAll(/\[(.*?)\]/g);
let data = [];
if (match) {
data = [...match].map((m) => m[1]);
}
return data
will return an array ["insert niche", "insert name", /* and so on */]
Omg, thanks you are a life saver. I was clicking around trying to figure out how to add that code in and I managed to get it to appear! Not bad for a complete beginner in Weweb who just learned what an array is last week…
How do i turn that array of things [x,y,z] into something that I can add more details to? If I’m not wrong, I have to create an array of objects, with each object having 3 properties, is that right? How am I supposed to do that in the workflows?
Now, I have this data grid of which is displaying an array of objects, with each object having “Mapping Label”, “Prompt”, and “Answer” properties (from just now).
When I click on the submit answers button, how do I get the answers to populate and replace everything in the square brackets (basically the reverse?).
So basically,
“Hello [a], you rock! Do you like [y]? What about [z]?” changes to become “Hello Sarah, you rock! Do you like Apples? What about Oranges?”
Create a variable to hold your result.
Then create a workflow, on click on your button.
On this workflow
First action need to set your result variable to your text with placeholder
add a Loop action, and bind the items property to your datatable array variable.
inside the loop, add a Set variable action, targeting your result variable. You can bind the value to a js formula using the replaceAll methods on your variable. Your pattern and value will be inside the loop property of your loop action
The replaceAll is use like this: value.replaceAll(mappingLabel, answer)
Value should be your result variable, mappingLabel the label from the current loop item, and answer the answer of your current loop item.