Z
Z
zlodiak2019-02-05 14:11:06
JavaScript
zlodiak, 2019-02-05 14:11:06

How to find the last part of an address?

There is a regular expression that looks for the part of the address string that matches the house number:

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

It definitely works .
But I can't use it in a script:
const addr = 'Россия, Республика Татарстан, Казань, улица Бутлерова, 41';
const regEx = new RegExp('((?<!(д(ом)?|стр(оение)?|/|-)\.?\s*\d*)\d+((,?\s*(к(ор(п(ус)?)?)?\.?)\s*\d+)|(\s*[а-я])|(\s*/\s*\d+))?\s*$)');
const beginStr = addr.match(regEx);
console.log('addr', addr)
console.log('regEx', regEx)
console.log('beginStr', beginStr)

This is expressed in the fact that the result in the console is displayed as null.
Please help me fix the script. LIVE DEMO here .

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2019-02-05
@zlodiak

when you use regular expressions in js using the constructor RegExp, you should escape slashes
i.e. not 1 slash \, but 2 \\
well, or use a literal notation
https://jsfiddle.net/nick_sdk/8zh4o7cn/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question