L
L
lexstile2021-08-30 22:36:13
JavaScript
lexstile, 2021-08-30 22:36:13

How to correctly compose a regular expression for a surname in Cyrillic in JS?

Requirements:

  • Cyrillic only
  • Possible character "-" - (hyphen)
  • Possible character " " (space)
  • Beginning and end of line - Cyrillic only
  • In the middle of the line, you can use a space or a hyphen, but no more than 1 in a row ("-" or "-" is also impossible)

Examples of valid strings:
  • "Ivanova"
  • "Ivanova Petrova"
  • "Ivanova-Petrova"

Examples of invalid strings:
  • "-Petrova"
  • "Petrova-"
  • "Ivanova-Petrova"
  • "Ivanova-Petrov"
  • "Ivanova - Petrova"
  • "Ivanova--Petrova"
  • "Ivanova Petrova"


I got this nonsense:
^[а-яё]*[а-яё]+[\-\s]?[а-яё]+$

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-08-30
@lexstile

/^[a-яё]+(?:[ -][a-яё]+)*$/i

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question