I want to implement a sort of Glossary in a Velocity template (using Javascript). The following is the use case:
- there are a large number of items whose description may contain references to predefined terms
- there is a list of terms which are defined -> allGlossary
- I want to automatically find and mark all items in the allGlossary list that appear in the description of all my items
Example:
allGlossary = ["GUI","RBG","fine","Color Range"]
Item Description: The interface (GUI) shall be generated using only a pre-defined RGB color range.
After running the script, I would expect the Description to look like this: "The interface (GUI) shall be generated using only a pre-defined RGB Color Range."
NOTE: even though "fine" does appear in the Description (defined), it shall not be marked.
I thought of splitting the description of each item into words but then I miss all the glossary items which have more than 1 word. My current idea is to look for each item in the list in each of the descriptions but I have the following limitations:
- I need to find only matches for the exact items in the 2 lists (single and multiple words)
- The search has to be case insensitive
- Items found may be at the beginning or end of the text and separated by various symbols: space, comma, period, parentheses, brackets, etc.
I have the following code which works but is not case-insensitive:
#set($desc = $item.description)
#foreach($g in $allGlossary)
#set($desc = $desc.replaceAll("\b$g\b", "*$g*"))
#end##foreach
Can someone please help with making this case-insensitive? Or does anyone have a better way to do this?
Thanks!
UPDATE: based on the answer below, I tried to do the following in my Velocity Template page:
#set($allGlossary = ["GUI","RGB","fine","Color Range"])
#set($itemDescription = "The interface (GUI) shall be generated using only a pre-defined RGB color range.")
<script type="text/javascript">
var allGlossary = new Array();
var itemDescription = "$itemDescription";
</script>
#foreach($a in $allGlossary)
<script type="text/javascript">
allGlossary.push("$a");
console.log(allGlossary);
</script>
#end##foreach
<script type="text/javascript">
console.log(allGlossary[0]);
</script>
The issue is that if I try to display the whole allGlossary Array, it contains the correct elements. As soon as I try to display just one of them (as in the example), I get the error Uncaught SyntaxError: missing ) after argument list
.