I want to implement a sort of Glossary in a Velocity template (using Javascript). The following is the use case:
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 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
.
You mentioned, that you are using JavaScript for these calculations. So one simple way would be to just iterate over your allGlossary
array and create a regular expression for each iteration and use this expression to find and replace all occurrences in the text.
To find only values which are between word boundaries, you can use \b
. This allows matches like (RGB)
or Color Range?
. To match case insensitive, you can use the /i
flag and to find every instance in a string (not just the first one), you can use the global flag /g
.
Dynamic creation of regular expressions (with variables in it) is only supported in JavaScript, if you use the constructor notation of a regular expression (don't forget to escape slashes). For static regular expressions, you could also use: /\bRGB\b/ig
. Here is the dynamic one:
new RegExp("\\b("+item+")\\b", 'gi');
Here is a fully functional example based on your sample string. It replaces every item of the allGlossary
array with a HTML wrapped version of it.
var allGlossary = ["GUI","RGB","fine","Color Range"]
var itemDescription = "The interface (GUI) shall be generated using only a pre-defined RGB color range.";
for(var i=0; i<allGlossary.length; i++) {
var item = allGlossary[i];
var regex = new RegExp("\\b("+item+")\\b", 'gi');
itemDescription = itemDescription.replace(regex, "<b>$1</b>");
}
console.log(itemDescription);
If this is not your expected solution, you can leave a comment below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With