Here is an example sentence:
क्या आप क्लोज़अप करते हैं
I want to extract the first word क्या from this sentence using Regex. I can do so in English by using (^\w+) but that doesn't work with other alphabets.
How should I proceed?
Here is an example sentence:
क्या आप क्लोज़अप करते हैं
I want to extract the first word क्या from this sentence using Regex. I can do so in English by using (^\w+) but that doesn't work with other alphabets.
How should I proceed?
You can add the u flag for Unicode support. That way you can specify patterns that include character properties, such as \p{N} for numbers in any language (Arabic, Chinese, ...).
const str = 'क्या आप क्लोज़अप करते हैं ';
console.log('Letters and accent marks: ' + str.match(/^[\p{L}\p{M}]+/u))
console.log('Anything but space: ' + str.match(/^[^\p{Zs}]+/u))
Result:
Letters and accent marks: क्या
Anything but space: क्या
Explanation:
^ to anchor at the beginning[\p{L}\p{M}]+ - one or more letters and accent marks[^\p{Zs}]+ - anything that is not a space (includes all Unicode spaces)u flag enables Unicode so that you can use \p{...} Unicode patternsSee details at https://javascript.info/regexp-unicode