Velocity RegEx for case insensitivity

I want to implement a kind of glossary in a speed template (using Javascript). The following is an example of use:

  • there are a large number of elements, the description of which may contain links to predefined terms
  • there is a list of terms that are defined β†’ allGlossary
  • I want to automatically find and tag all the items in the allGlossary list that appear in the description of all my items.

Example:

allGlossary = ["GUI", "RBG", "fine", "Color Range"]

Description of the item . An interface (GUI) should be generated using only a specific range of RGB colors.

After running the script, I expect the description to look like this: "An interface ( GUI ) should be generated using only the predefined RGB color range ."

NOTE : even if the message β€œfine” appears in the description (de fine d), it should not be marked.

I thought about dividing the description of each element into words, but then I skip all the dictionaries containing more than one word. My current idea is to search for every item in the list in each of the descriptions, but I have the following limitations:

  • I need to find only matches for exact elements in 2 lists (one and several words)
  • The search must be case insensitive.
  • Items found can be at the beginning or end of the text and separated by various characters: space, comma, period, parentheses, brackets, etc.

I have the following code that works, but is not case sensitive:

#set($desc = $item.description) #foreach($g in $allGlossary) #set($desc = $desc.replaceAll("\b$g\b", "*$g*")) #end##foreach 

Can anyone help in creating 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 on the 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 problem is that if I try to display the entire allGlossary array, it contains the correct elements. As soon as I try to display only one of them (as in the example), I get the error message Uncaught SyntaxError: missing ) after argument list .

+5
source share
1 answer

You mentioned that you use JavaScript for these calculations. Thus, one simple way would be to simply allGlossary over the allGlossary array and create a regular expression for each iteration and use that expression to find and replace all occurrences in the text.

To find only meanings that are between word boundaries, you can use \b . Does this allow matches like (RGB) or Color Range? . To match case insensitive cases, you can use the /i flag and find each instance in the line (and not just the first), you can use the /g global flag.

Dynamically creating regular expressions (with variables in it) is only supported in JavaScript if you use the regex constructor notation (remember to avoid the slash). For static regular expressions, you can also use: /\bRGB\b/ig . Here is the dynamic:

 new RegExp("\\b("+item+")\\b", 'gi'); 

Here is a fully functional example based on your sample string. It replaces each element of the allGlossary array allGlossary its HTML wrapper.

 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 decision, you can leave a comment below.

+4
source

Source: https://habr.com/ru/post/1264184/


All Articles