Skip to content Skip to sidebar Skip to footer

JavaScript Regex To Get First Character Of Each Word In A Sentence (Persian, And English Sentence)

Suppose I have the following string: var englishSentence = 'Hellow World'; var persianSentence = 'گروه جوانان خلاق'; For english I use from following regex, but how

Solution 1:

Root cause

There is no way to match a Unicode word boundary, \b is not Unicode aware even in ECMA 2018.

Solutions

For ECMA2018 compatible browsers (e.g. the latest versions of Chrome as of April 2018) you may use:

var englishSentence = 'Hellow World';
var persianSentence = 'گروه جوانان خلاق';
var reg = /(?<!\p{L}\p{M}*)\p{L}\p{M}*/gu;
console.log(englishSentence.match(reg));
console.log(persianSentence.match(reg));

Details

  • (?<!\p{L}\p{M}*) - a negative lookbehind that fails the match if there is a Unicode letter followed with 0+ diacritics
  • \p{L}\p{M}* - a Unicode letter followed with 0+ diacritics
  • gu - g - global, search for all matches, u - make the pattern Unicode aware.

If you need the same functionality in older/other browsers, use XRegExp:

function getFirstLetters(s, regex) {
  var results=[], match;
  XRegExp.forEach(s, regex, function (match, i) {
    results.push(match[1]);
  });
  return results;
}
var rx = XRegExp("(?:^|[^\\pL\\pM])(\\pL\\pM*)", "gu");
console.log(getFirstLetters("Hello world", rx));
console.log(getFirstLetters('گروه جوانان خلاق', rx));
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.js"></script>

Details

  • (?:^|[^\\pL\\pM]) - a non-capturing group that matches the start of the string (^) or any char other than a Unicode letter or diacritic
  • (\\pL\\pM*) - Group 1: any Unicode letter followed with 0+ diacritics.

Here, we need to extract Group 1 value, hence .push(match[1]) upon each match.


Solution 2:

You can split by space(s) and then get the first character of each item

var output = sentence.split( /\s+/ ).map( s => s.charAt(0) ).join("")

Demo

var fnGetFirstChar = (sentence) => sentence.split( /\s+/ ).map( s => s.charAt(0) ).join("");

var englishSentence = 'Hellow World';
var persianSentence = 'گروه جوانان خلاق';

console.log( fnGetFirstChar( englishSentence ) );

console.log( fnGetFirstChar( persianSentence ) );

Solution 3:

If you're doing this in code, one way of doing it is with

(?:\s|^)(\S)

It matches a non white space character (\S) preceded by a white space OR beginning of string (\s|^), capturing the non white space character to capture group 1.

var sentence  = 'Hello World\n'+
                'گروه جوانان خلاق',
    re        = /(?:\s|^)(\S)/g,
    result = '';
    
while( m = re.exec(sentence) )
{
  result += m[1];
};

console.log( result );

Solution 4:

You'd better use a character range from آ to ی along with a-z since a word boundary in JS doesn't recognize multibyte letters while in most flavors it does.

console.log(
  "سلام حالت چطوره؟".match(/( |^)[آ-یa-z](?=[آ-یa-z])/gi).map(x => x.trim()).join('')
)

console.log(
  "این یک test است".match(/( |^)[آ-یa-z](?=[آ-یa-z])/gi).map(x => x.trim()).join('')
)

Breakdown:

  • (?: |^) Match a space or beginning of input string
  • [آ-ی] Match a character from Farsi
  • (?= Start a positive lookahead
    • [آ-ی] If followed by another Farsi character
  • ) End of positive lookahead

Note: character range from آ to ی has more than Farsi alphabets in it (some Arabic letters too) for a precise match (I doubt if you use those letters anywhere though) use a solid character class:

[اآبپتثجچحخدذرزژسشصضطظعفقگکلمنوهی]

console.log(
    "سلام دوست من".match(/( |^)[اآبپتثجچحخدذرزژسشصضطظعفقگکلمنوهیa-z](?=[اآبپتثجچحخدذرزژسشصضطظعفقگکلمنوهیa-z])/gi).map(x => x.trim()).join('')
)

Post a Comment for "JavaScript Regex To Get First Character Of Each Word In A Sentence (Persian, And English Sentence)"