I am trying to test whether a string can be converted into a number in FreeMarker. For example, "123" and "3.14" can be converted, but "foo" can't. I know that I can test for this by using the number method on the string (e.g. "123"?number
) and seeing whether it produces an error, but I am looking for a way to test for this without causing an error.
I tried ?matches("^\d+$")
, and it works fine for integers, but I am looking for something that works on all numbers.
I can probably do it using a more sophisticated regex, but I am wondering if there is a simpler way.
The simpler way is to not do it in FreeMarker :-) This sounds like something controller (or method on model) should be doing rather than view template. That said, you have a few options:
<#attempt> / <#recover>
block.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