Skip to main content

extractChars

A function that extracts characters from a given string according to the specified options.

By default, it extracts letters only ({ letters: true }). When options is passed, any option left out is treated as false, so set letters: true explicitly if you also want to extract letters. The following options can be configured:

  • letters: Specifies whether to extract letters from the string. Combining marks (accents, etc.) are extracted together.
  • numbers: Specifies whether to extract numbers from the string.
  • specialCharacters: Specifies whether to extract special characters from the string. ZWJ/variation selectors that join emoji sequences are extracted together.
  • whiteSpace: Specifies whether to extract whitespace from the string.

It does not break combining marks in NFD-normalized strings or ZWJ-joined emoji sequences, and control characters that distort display (RLO, ZWSP, etc.) are always removed regardless of the options.


Code​

πŸ”— View source code


Interface​

typescript
interface ExtractCharsOptions {
letters?: boolean;
numbers?: boolean;
specialCharacters?: boolean;
whiteSpace?: boolean;
}

function extractChars(
value: string,
options?: ExtractCharsOptions // default: { letters: true }
): string;

Usage​

typescript
import { extractChars } from '@modern-kit/utils';

const input = 'Hello, δΈ–η•Œ! μ•ˆλ…•ν•˜μ„Έμš” 123 こんにけは $100 + 200 = 300! πŸ˜„';

const letterOnly = extractChars(input); // 'HelloδΈ–η•Œμ•ˆλ…•ν•˜μ„Έμš”γ“γ‚“γ«γ‘γ―'

const lettersAndWhiteSpace = extractChars(input, {
letters: true,
whiteSpace: true,
}); // 'Hello δΈ–η•Œ μ•ˆλ…•ν•˜μ„Έμš” こんにけは '

const numbersAndSpecialCharacters = extractChars(input, {
numbers: true,
specialCharacters: true,
}); // ',!123$100+200=300!πŸ˜„'

// Combining marks are preserved with letters (NFD-normalized string)
extractChars('JosΓ©'.normalize('NFD'), { letters: true }); // 'JosΓ©'

// Emoji sequences are not broken apart
extractChars('κ°€μ‘± πŸ‘¨β€πŸ‘©β€πŸ‘§', { specialCharacters: true }); // 'πŸ‘¨β€πŸ‘©β€πŸ‘§'