My goal is to dynamically add and remove strings to a textarea when values in a table are clicked. The functionality should allow users to select and deselect values in the table, with the selected values adding or removing themselves from the textarea. It is important that the values added to the textarea remain as plain strings without any additional characters.
The values being added may contain any characters and could potentially have one value as a substring of another. For example, values like HOLE 1
, HOLE 11
, cutaway
, cut
, Commentator (SMITH, John)
, and (GOAL)
are all possibilities.
When a value is clicked to deselect and remove it from the textarea, I am currently using a regex to find and replace the value. However, the current regex pattern works for most cases except for strings starting with a bracket, such as (GOAL)
. Adding a word boundary selector \b
to the regex improves the matching for strings starting with a bracket, but it may affect the matching of substrings within the text.
I am wondering if there is a way to improve the regex or perhaps explore other methods to achieve the desired functionality. Here is a CodePen example demonstrating the adding and removing values from the table.