Z
Z
zlodiak2019-02-19 13:33:33
JavaScript
zlodiak, 2019-02-19 13:33:33

Why is regEx not working in firefox?

In the latest chrome browser, I use the following regular expression without any problems:

/((?<!(д(ом)?|стр(оение)?|\/|-)\.?\s*\d*)\d+((,?\s*(к(ор(п(ус)?)?)?\.?)\s*\d+)|(\s*[а-я])|(\s*\/\s*\d+))?\s*$)/

But firefox browser doesn't understand such regex. It prints the following error message to the console:
SyntaxError: invalid regexp group

Please help me rewrite the regular expression so that the result remains the same, but firefox does not crash.
LIVE DEMO is here.
The code:
// this function transform
// Татарстан, г. Казань, ул. Баумана, 36
// to
// Татарстан, г. Казань, ул. Баумана, д. 36

function transform(addr) {
  const regEx = /((?<!(д(ом)?|стр(оение)?|\/|-)\.?\s*\d*)\d+((,?\s*(к(ор(п(ус)?)?)?\.?)\s*\d+)|(\s*[а-я])|(\s*\/\s*\d+))?\s*$)/;
  const endStr = addr.match(regEx);
  let result;
  let ret;

  if(endStr && endStr[0]) {
    result = addr.replace(endStr[0], 'д. ' + endStr[0]);
  } else {
    result = addr;
  }

  return result;
}

console.log(transform('Татарстан, г. Казань, ул. Баумана, 36'));

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
dodo512, 2019-02-19
@zlodiak

function transform(addr) {
    const regEx = /((д(ом)?|стр(оение)?|\/|-)\.?\s*|)\d+((,?\s*(к(ор(п(ус)?)?)?\.?)\s*\d+)|(\s*[а-я])|(\s*\/\s*\d+))?\s*$/;
    let result = addr.replace(regEx, function(m0, m1) {
        if (m1) {
            return m0;
        } else {
            return 'д. ' + m0;
        }
    });
    return result;
}

console.log(transform('Татарстан, г. Казань, ул. Баумана, 36'));

S
Sergey Sokolov, 2019-02-19
@sergiks

FireFox does not yet support lookbehind: (?<= )and (?<! ), which are introduced in ECMA2018.

M
Michael, 2019-02-19
@nyakove

Firefox does not support "negative look-behind". https://bugzilla.mozilla.org/show_bug.cgi?id=1225665

F
Flying, 2019-02-19
@Flying

regex101 can't parse your regex either, so it's probably a syntax issue. Yes, and parsing the address with a regular expression is a so-so idea, what will you do if the sequence of elements in the address is different or if there are typos?
Perhaps you should consider using external services that normalize addresses? For example, I used DaData in one project and they work really well (not advertising :) ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question