I am a little stumped by this but I want to achieve the following.
I have a large string and within that string I want to match against an array of strings and replace with markup tags.
Take the following string:
The quick brown fox jumps over a lazy dog.
This is my list of strings ( which could be a whole sentence not just a word) I wish to match to the body of text:
['quick', 'brown', 'lazy dog', '.']
My result I am trying to achieve:
// ['The', <span>quick</span>, '<span>brown</span>', 'fox jumps over a' '<span>lazy dog</span>', '<span>.</span>]
Caveats and additional notes:
- Avoid using DOM manipulation like addClass, innerHTML, appendChild, as I would like to come up with an elegant solution within the render function of a component with in React
- Not to use a complete string regex solution as I am wanting to insert react DOM elements instead of manipulating and parsing the dom after the solution
- I want to attach additional information to the span tags IE the react DOM elements to filter by these wrapped strings, for example ID's or native react events to that element
- I must cater for matching sentences not just individual words.
- No browser requirements
- List of strings will always be unique and have no overlaps
- Not all strings in the list are within the body of text ( ;) )
Kinda the dataset I would be dealing with: https://codepen.io/nigel_manny/pen/omjxrx
Goodluck and thank you!
['quick', 'quick brown', 'lazy dog', '.']?