K
K
Konstantin Vasilyev2020-12-04 06:13:08
JavaScript
Konstantin Vasilyev, 2020-12-04 06:13:08

How to count the number of characters of a word from a regular expression in Javascript?

It is necessary that words containing characters from the array are replaced with stars. But the character count does not work: the variable is not defined.

window.onload = function() {
s = document.body.innerHTML;

let abc = 'abc';
 


let regularBadWordsArray = [
'слово-1',
'слово-2'
];


for (let i = 0; i < regularBadWordsArray.length; i++) {
let regularBadWord = new RegExp(
    '\\B' + '[а-яА-Я]*' + regularBadWordsArray[i] + '[а-яА-Я]*' + '\\B',
    'gi'
  );  
  
s = s.replace(regularBadWord, '*'.repeat(regularBadWord.length)); 


console.log(regularBadWord.length);
}

document.body.innerHTML=s;

};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-12-04
@KonstantinVasilev

s = s.replace(regularBadWord, '*'.repeat(regularBadWord.length));

Change to
s = s.replace(regularBadWord, m => '*'.repeat(m.length));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question