I’m facing at first sight a fairly trivial task but during a day I didn’t succeed to advance somehow in it. I need to process a string in velocity template.
The string has such view:
Maj - [at552] - asdfghjkksfgh <br /> Avg - [at553] - asdfghjkksfgh <br /> etc
I need to replace text among [ ] symbols with following text:
<a href="http://someaddress/at522">at522</a> (at522 is specific for
each [ ] block)
I tried so many approaches and none of them worked, so I don’t even know what snippets of code could I post here. An important detail is that I don’t have access to the java part of the application, just the object that is passed into the velocity template. I can’t use JavaScript because this a template for email. I would really appreciate every advice.
UPDATE
Here is the code that works fine for the first occurence of [ ]
#set ($scopeComment = $issue.getCustomFieldValue("customfield_10201").replaceAll("\n", "<br />"))
#set ($start = $scopeComment.indexOf("["))
#set ($end = $scopeComment.indexOf("]"))
#set ($substr = $scopeComment.substring($start, $end))
#set ($scopeComment = $scopeComment.replace($substr, "<a href=\"localhost/$substr\">$substr</a>"))
But I can’t make it work in a loop for all [ ] blocks
Here is one of the approach I’m trying
#foreach ($index in [0..$scopeComment.length()])
#set($nextIndex = $index.index+1)
#set($curChar = $scopeComment.substring($index.index, $nextIndex))
#if ($curChar.equals("["))
#set ($start = $count+1)
#set ($end = $scopeComment.indexOf("]", count))
#set ($sub = $scopeComment.substring($start, $end))
$sub <br/>
#end
#end
A RegExp will work in this case:
Note that I used single quotes, and single backslash. Unlike Java, Velocity behaves differently when double or single quotes are used.
There are many errors in your code. For example, what’s
$index.indexsupposed to be?$indexis an integer, so you should just use$indexin your checks. Also, you use$countbut that one isn’t defined anywhere, you probable mean$indexas well.