1.1.0 • Published 9 months ago

discord-regexp v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Discord-RegExp

Useful Regular Expressions for Discord & more.

Features

  • Useful for auto-moderation bots.
  • 19+ Regular Expressions.
  • Helpful functions for extractors.
  • Discord Markdown related stuff.

Install

npm install discord-regexp
yarn add discord-regexp
pnpm add discord-regexp

Import

TypeScript:

import { ... } from 'discord-regexp';

JavaScript (CommonJS):

const { ... } = require('discord-regexp');

Example usage

Useful built-in functions:

import {
    extractCodeblock,
    extractDiscordTimestamp,
    extractInternalLink,
    customRegExp
} from 'discord-regexp';

extractCodeblock(`\`\`\`js\nconsole\n\`\`\``); // → { language: 'js', content: 'console' }
extractCodeblock(`\`\`\`\nconsole\n\`\`\``); // → { language: null, content: 'console' }
extractCodeblock('Hello world'); // → null

extractDiscordTimestamp('<t:69420>'); // → { timestamp: 69420, type: null }
extractDiscordTimestamp('<t:69420:d>'); // → { timestamp: 69420, type: 'd' }
extractDiscordTimestamp('Hello world'); // → null

extractMarkdownInternalLink('[Click here](https://www.google.com/)'); // → { text: 'Click here', url: { type: 'link', string: 'https://www.google.com/' } }
extractMarkdownInternalLink('[Click here](#license)'); // → { text: 'Click here', url: { type: 'heading', string: '#license' } }
extractMarkdownInternalLink('[Click here](A text)'); // → { text: 'Click here', url: { type: undefined, string: 'A text' } }
extractMarkdownInternalLink('Hello world'); // → null

customRegExp(['a', 'b', 'c']); // → /a|b|c/
customRegExp(['a', 'b', 'c'], 'g'); // → /a|b|c/g

customRegExp(['a', 'b', 'c']).test('a'); // → true
customRegExp(['a', 'b', 'c']).test('d'); // → false

Regular expressions:

Note: By default, these patterns has no flags. You can use the function addFlags() to add some flags:

import { addFlags, regexpCapsLock } from 'discord-regexp';

console.log(regexpCapsLock); // → /[A-Z]/

const newPattern = addFlags(regexpCapsLock, 'g');

console.log(regexpCapsLock); // → /[A-Z]/g

Learn more about RegExp flags: Click here

import { regexp... } from 'discord-regexp';

regexpUserMention.test('<@!123456789>'); // → true
regexpChannelMention.test('<#123456789>'); // → true
regexpRoleMention.test('<?123456789>'); // → false

regexpIPv4.test('000.00.00.000'); // → true

regexpQuotedText.test('Hello'); // → false
regexpQuotedText.test('> Hello'); // → true

regexpLines.test('Line 1 \n Line 2'); // → true

regexpMarkdownHeadings.test('# H1'); // → true
regexpMarkdownHeadings.test('## H2'); // → true
regexpMarkdownHeadings.test('P'); // → false

regexpMarkdownInternalLinks.test('https://www.google.com'); // → false
regexpMarkdownInternalLinks.test('[Your Text](Your URL)'); // → true

regexpBold.test('**Ayo**'); // → true
regexpItalic.test('*Hmm*'); // → true
regexpCode.test('`A code`'); // → true

regexpCapsLock.test('WHAT'); // → true
regexpCapsLock.test('What'); // → false
regexpCapsLock.test('what'); // → false

regexpNumbersOnlyInString.test('12345'); // → true
regexpNumbersOnlyInString.test('12a45'); // → false

License

The MIT License.