G
G
Galdar Turin2020-06-18 17:37:02
Node.js
Galdar Turin, 2020-06-18 17:37:02

Why does regex work like this?

var symbol = "s cchh'shchcz'' ";

console.log(symbol.split(/(sh|sch|ch|cz|yu|ya|yo|zh|''|'|.)/))

[
  '', 's',  '', ' ',  '', 'c',
  '', 'ch', '', 'h',  '', "'",
  '', 'sh', '', 'ch', '', 'cz',
  '', "''", '', ' ',  ''
]


Puts empty values ​​in the array after one character, why is that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Klimenko, 2020-06-18
@Galdar

Because you wrote it like this =)

/(sh|sch|ch|cz|yu|ya|yo|zh|''|'|.)/
                                ^
                             // └ Сопоставляется с любым символом

This is how it works correctly
/(sh|sch|ch|cz|yu|ya|yo|zh|''|')/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question