0

It seems to me that jQuery RegExp can't handle matching in whole string.

Here is regex pattern and function for test string:

var billMonthYearRg = new RegExp("^\D*([0-9]+)\s?(-|\/)\s?([0-9]{4})\D*$", "gi");
$('#Description').on('change', function () {
  var matches = billMonthYearRg.exec($('#Description').val());
  if (matches != null && matches.length > 3) {
    $('#BillMonth').val(matches[1]);
    $('#BillYear').val(matches[3]);
  }
});

I try to match month-year from text, but only if it occurs one time and if there is no more numeric values, Eg. 'Something non digit text 11-2222 Something non digit text'.

Pattern is OK, when we move ^ at the begging and $ at the end of pattern, but then we get match if there mm-yyyy occurs more the ones.

As input for 'Description' for, we use:

    <textarea id="Description" name=""></textarea>
    <input id="BillMonth" type="number" name="BillMonth" />
    <input id="BillYear" type="number"  name="BillYear"/>

We were test regext on https://regex101.com/ and it work fine, but on site it is not working.

We use jQuery JavaScript Library v2.1.4

Igor
  • 381
  • 3
  • 17

0 Answers0