I've got a string like this:
"Hello World! It's me, Chelsea."
And a list of strings.
["Hello", "World", "Chelsea"]
I'd like to dynamically wrap the matching strings in Vue components. Individually, it looks like this:
<MyComponent :text="Hello"/> <MyComponent :text="World"/>! It's me, <MyComponent :text="Chelsea"/>.
The solution for this could be something like the following (thanks Ulysse BN):
<template v-for="s in string.split(/\b/)">
<MyComponent v-if="list.includes(s)" :string="s"/>
<span v-else>{{ s }}</span>
</template>
But we run into problems where we have multi-word strings in our list (e.g. "It's me") and more specifically, overlapping words. If we added to the list of strings "Hello World", here's what the ideal result looks like:
<MyComponent :text="Hello World">
<MyComponent :text="Hello"/>
<MyComponent :text="World"/>!
</MyComponent>
It's me, <MyComponent :text="Chelsea"/>.
How can I achieve this functionality? I have a hunch it involves v-for and some kind of recursive function, but how I can't say.