Skip to content

fabiospampinato/regexp-get-prefix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RegExp Get Prefix

A function that extracts the set of possible first consumed characters from a regular expression.

If any character or no character could be matched, or if over 1000 different characters could be matched, or if the library is not quite sure (e.g. when using unicode property escapes or references), null is returned instead.

All features up to ES2025 are supported.

Install

npm install regexp-get-prefix

Usage

import getPrefix from 'regexp-get-prefix';

// Let's extract the set of possible first consumed characters

getPrefix ( /foo|bar/ ); // => Set { 'f', 'b' }
getPrefix ( /f?oo/ ); // => Set { 'f', 'o' }
getPrefix ( /o?k/i ); // => Set { 'o', 'O', 'k', 'K' }
getPrefix ( /[2-8]/ );   // => Set { '2', '3', '4', '5', '6', '7', '8' }

// Let's see some cases where we aren't able to narrow it down

getPrefix ( /^/ ); // => null
getPrefix ( /a?/ ); // => null
getPrefix ( /./ ); // => null

License

MIT © Fabio Spampinato

About

A function that extracts the set of possible first consumed characters from a regular expression.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages