Answer the question
In order to leave comments, you need to log in
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*$)/
SyntaxError: invalid regexp group
// 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
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'));
FireFox does not yet support lookbehind: (?<= )
and (?<! )
, which are introduced in ECMA2018.
Firefox does not support "negative look-behind". https://bugzilla.mozilla.org/show_bug.cgi?id=1225665
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 questionAsk a Question
731 491 924 answers to any question