A
A
Anna2018-08-22 12:21:20
JavaScript
Anna, 2018-08-22 12:21:20

How to compose a regular expression for selecting words from a dictionary according to a certain pattern?

It is necessary to select words from the dictionary, in which first there is one consonant from the set [bvgdzhzk], then one or more of any vowels, then again one consonant from the set [bvgdzhzk], and then an arbitrary number of any letters. She wrote this:

var str = 'человек время дело жизнь день рука раз работа слово место лицо друг';
str=str.match(/[бвгджзк]{1}[аяеэюуёоиы]{1}[бвгджзк]{1}[а-яА-ЯёЁ]{0,}/gi)
console.log(str)

I get Array(2) ["age", "life"]. Why is it giving out a word fragment?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-08-22
@LShAnka

Why is it giving out a word fragment?

Because splitting a string by words is only in your head, there is nothing about word boundaries in your regular expression. There is a special character \bdenoting word boundaries, but it is not applicable in this case, since you have Cyrillic characters here. You can think of a word boundary as the beginning of a line, or a whitespace character, or the end of a line. Like this:
But in general - somehow it is not very clear why such difficulties. You say you have some kind of dictionary - well, put the words in an array, and check for an exact match, you won't need to worry about word boundaries.

S
Stalker_RED, 2018-08-22
@Stalker_RED

Why is it giving out a word fragment?

Because you don't check for word boundaries. In general, checking the word boundary for the Cyrillic alphabet is another quest. It's easier to split your list by spaces, check each word separately, and glue it back together.
https://regex101.com/r/PprlqG/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question