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

How to get the first and last characters of a word from a regular expression in Javascript?

The console shows that the variables are not defined.

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

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

  
// Первый и последний символы

let rbwFirstLetter = regularBadWord[0];  
let rbwLastLetter = regularBadWord[-1];

console.log(rbwFirstLetter);
console.log(rbwLastLetter);

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eyyuioa, 2020-12-04
@eyyuioa

string[string.length - 1]
string.slice(-1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question