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β
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 }); // 'π¨βπ©βπ§'