2

I am trying to check if the first letter in a string is a Cyrillic letter.

Here is what I have until now, but the problem is that if the string starts with a digit it also fires the pattern:

$(document).on('keydown keyup', '#userBox', function() {
  $('#result').html('');
    if (/[a-zA-Z]*[^A-Za-z \d]+[a-zA-Z]*/.test(this.value)) {
      $('#result').html('Cyrillic');
    } else {
      $('#result').html('Non-Cyrillic');
    }
    if ( $(this).val().length === 0) {
          $('#result').html('');
        }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="userBox" autocomplete="off" type="text"  autofocus="true" placeholder="Type your message"> <span id="result"></span>

also using the solution from this post How to match Cyrillic characters with a regular expression will produce the same result

$(document).on('keydown keyup', '#userBox', function() {
  $('#result').html('');
    if (/[\p{IsCyrillic}]/.test(this.value)) {
      $('#result').html('Cyrillic');
    } else {
      $('#result').html('Non-Cyrillic');
    }
    if ( $(this).val().length === 0) {
          $('#result').html('');
        }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="userBox" autocomplete="off" type="text"  autofocus="true" placeholder="Type your message"> <span id="result"></span>
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
lStoilov
  • 1,256
  • 3
  • 14
  • 30
  • So, all you want to is to match the first Cyrillic letter? `if (/^\p{Script=Cyrl}/u.test(this.value))`? – Wiktor Stribiżew Jan 19 '22 at 10:03
  • @WiktorStribiżew That will not do it... I want to apply the rule only to the first letter that appears in the string. So, this `234 test` should be Latin and this `234 тест` should be Cyrillic, although, both start with a number. – lStoilov Jan 20 '22 at 17:24
  • 1
    Ok, `if (/^\P{L}*\p{Script=Cyrl}/u.test(this.value))`? – Wiktor Stribiżew Jan 20 '22 at 21:11

1 Answers1

1

You can use

/^\P{L}*\p{Script=Cyrl}/u

See the regex demo. Note the /u flag that makes it possible to use Unicode property/category classes inside ECMAScript 2018+ compliant regular expressions. Details:

  • ^ - start of string
  • \P{L}* - zero or more chars other than letters
  • \p{Script=Cyrl} - a Cyrillic char

$(document).on('keydown keyup', '#userBox', function() {
  $('#result').html('');
    if (/^\P{L}*\p{Script=Cyrl}/u.test(this.value)) {
      $('#result').html('Cyrillic');
    } else {
      $('#result').html('Non-Cyrillic');
    }
    if ( $(this).val().length === 0) {
          $('#result').html('');
        }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="userBox" autocomplete="off" type="text"  autofocus="true" placeholder="Type your message"> <span id="result"></span>

Bonus:

To make sure the first char of a string is a Cyrillic char use

/^\p{Script=Cyrl}/u

If you want to support leading optional whitespaces before the first char of a string that is a Cyrillic char:

/^\s*\p{Script=Cyrl}/u
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563