Reputation: 1339
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>
Upvotes: 2
Views: 183
Reputation: 627082
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
Upvotes: 1