2

My Regular expression is not working properly in neos project. Regular expression for DD/MM/YYYY (only 19XX-20XX)

var date_regex = /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/\/(19|20)\d{2}$/ ;
return date_regex.test(testDate);

I resolved the issue with this

var date_regex = /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/(19[0-9][0-9]|20[0-9][0-9])$/;

why neos remove the '{2}', Is it any problem with my new Regular expression

Your help would be appreciated

Georg Ringer
  • 7,779
  • 1
  • 16
  • 34
  • 1
    Did you get any exception when using `{2}`? – Wiktor Stribiżew Feb 11 '16 at 07:27
  • No. when i console the regular expression it become /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/\/(19|20)\d$/ –  Feb 11 '16 at 07:34
  • 1
    Most probably the `{...}` is parsed by Typo3 as Typo3 syntax, and thus you cannot used limited quantifiers inside a RegExp object. Did you try with `RegExp`? `var date_regex = RegExp("^(0[1-9]|1\\d|2\\d|3[01])/(0[1-9]|1[0-2])//(19|20)\\d{2}$");`. Also, the usual way to escape meaningful braces is by doubling them. Please try. – Wiktor Stribiżew Feb 11 '16 at 07:34
  • Ok Thanks for your comments. that is also not working. Do you have any short hand for this (19[0-9][0-9]|20[0-9][0-9]) –  Feb 11 '16 at 07:43
  • 2
    You can shorten it as `((19|20)\d\d)`. – Wiktor Stribiżew Feb 11 '16 at 07:45
  • In any case, the problem is *not* with the regular expression. – Wiktor Stribiżew Feb 11 '16 at 08:50

1 Answers1

2

In TYPO3 - Fluid template engine the curly brackets are used for view variables so using JavaScript within the view often produces errors as parser cannot guess if this is your Fluid's var or some JS syntax.

You have two solutions, first is to move whole JS into static file and include it in the header as ususally: <script src="/path/to/your/file.js"></script>

Second solution is escaping whole JS with cdata like:

<script>
<![CDATA[

    var date_regex = /^(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[0-2])\/\/(19|20)\d{2}$/ ;
    // ....

]]>
</script>
biesior
  • 55,576
  • 10
  • 125
  • 182