S
S
Sergey Ilyin2020-05-16 16:02:48
Python
Sergey Ilyin, 2020-05-16 16:02:48

Did you correctly translate the Python function into JS?

there is a function:

def text_cleaner_fio(text):
    text = re.sub( r'[\W]+', ' ', text ) # удаление лишних символов
    text = re.sub( r'([А-Я]{1}[а-яё]{0,23} [А-Я]{1}[а-яё]{0,23} [А-Я]{1}[а-яё]{0,23})|( [A-Z]{1}[a-z]{1,23} [A-Z]{1}[a-z]{1,23})$', ' FIO ', text ) # замена учеток
    return text


js is needed.
is it right:

function text_cleaner_fio(text){
    text.replace(/[\W]+/, ' '); # удаление лишних символов
    text.replace(/([А-Я]{1}[а-яё]{0,23} [А-Я]{1}[а-яё]{0,23} [А-Я]{1}[а-яё]{0,23})|( [A-Z]{1}[a-z]{1,23} [A-Z]{1}[a-z]{1,23})$/, 'FIO'); # замена учеток
    return text;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Derepko, 2020-05-16
@sunsexsurf

The peculiarity of regular expressions is that they represent a separate language in which they are written. That is, regular expression in python = regular expression in any other language.
The answer to your question is yes, correct.
You can also combine method calls:

function text_cleaner_fio(text){
    return text
        .replace(/[\W]+/, ' ') # удаление лишних символов
        .replace(/([А-Я]{1}[а-яё]{0,23} [А-Я]{1}[а-яё]{0,23} [А-Я]{1}[а-яё]{0,23})|( [A-Z]{1}[a-z]{1,23} [A-Z]{1}[a-z]{1,23})$/, 'FIO'); # замена учеток
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question