UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

22 lines (21 loc) 1.84 kB
import { getRegExpSource } from "../../util/regexp.js"; export const BLOCK_CONTENT_REGEXP = "[\\s\\S]*?"; // Block content (shortest run of any character). export const BLOCK_SPACE_REGEXP = "\\s"; // Block whitespace (run of any whitespace character). export const BLOCK_START_REGEXP = "(?:\\s*\\n|^)"; // Start of block (start of string, or one linebreak). export const BLOCK_END_REGEXP = "\\s*(?:$|\\n\\s*\\n)"; // End of block (end of string, or two linebreaks, trimmed whitespace). export const LINE_CONTENT_REGEXP = "[^\\n]*?"; // Line content (shortest run of any character except newline). export const LINE_SPACE_REGEXP = "[^\\n\\S]"; // Line whitespace (run of any whitespace character except newline). export const LINE_START_REGEXP = BLOCK_START_REGEXP; // Start of line (start of string, or one linebreak). export const LINE_END_REGEXP = `${LINE_SPACE_REGEXP}*(?:\\s*\\n|$)`; // End of line (end of string, or one linebreak, trimmed whitespace). export const WORD_CONTENT_REGEXP = "[\\p{L}\\p{N}]+"; // Word content (at least one character that is a letter or number). export const WORD_START_REGEXP = "(?<![\\p{L}\\p{N}])"; // Start of word (previous character is not a letter or number). export const WORD_END_REGEXP = "(?![\\p{L}\\p{N}])"; // End of word (next character is not a letter or number). export function getBlockRegExp(content, start = BLOCK_START_REGEXP, end = BLOCK_END_REGEXP) { return new RegExp(`${start}${getRegExpSource(content)}${end}`); } export function getLineRegExp(content = LINE_CONTENT_REGEXP, end = LINE_END_REGEXP, start = LINE_START_REGEXP) { return new RegExp(`${start}${getRegExpSource(content)}${end}`); } export function getWordRegExp(content = WORD_CONTENT_REGEXP, start = WORD_START_REGEXP, end = WORD_END_REGEXP) { return new RegExp(`${start}${getRegExpSource(content)}${end}`); }