Skip to main content

excludeChars

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

It is the exact opposite of extractChars, which keeps the specified types: it strips out the specified types instead.

By default, it excludes 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 exclude letters. The following options can be configured:

  • letters: Specifies whether to exclude letters from the string. Combining marks (accents, etc.) are excluded together.
  • numbers: Specifies whether to exclude numbers from the string.
  • specialCharacters: Specifies whether to exclude special characters from the string.
  • whiteSpace: Specifies whether to exclude whitespace from the string.

Code​

πŸ”— View source code


Interface​

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

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

Usage​

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

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

const withoutLetters = excludeChars(input); // ', ! 123 $100 + 200 = 300! πŸ˜„'

const withoutLettersAndWhiteSpace = excludeChars(input, {
letters: true,
whiteSpace: true,
}); // ',!123$100+200=300!πŸ˜„'

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

// Combining marks are not broken apart
excludeChars('JosΓ©'.normalize('NFD'), { numbers: true }); // 'JosΓ©'