Covert Oniguruma-flavor Regexp to JavaScript native RegExp.
Supported Oniguruma features:
[:alnum:],[:alpha:]etc. POSIX bracket expressions(?x)extended, free-spacing mode(?i:..)flags modifiers
npm i oniguruma-to-jsimport { onigurumaToRegexp } from 'oniguruma-to-js'
const re = onigurumaToRegexp(`[[:alnum:]_]+`)
console.log(re) // /^[a-zA-Z0-9_]+$/import { construct, syntaxLowering } from 'oniguruma-to-js'
const pattern = syntaxLowering(`(?x:
\d+ # Match one or more digits
\s* # Match zero or more whitespace characters
[A-Z]+ # Match one or more uppercase letters
)`)
console.log(pattern) // "\d+\s*[A-Z]+"
const re = construct(pattern)
console.log(re) // /\d+\s*[A-Z]+/Traverse all the regex patterns in a TextMate grammar, and apply syntaxLowering to lower the syntax.
import { loweringTextmateGrammar } from 'oniguruma-to-js/textmate'
import grammar from '../path/to/grammars/json.json'
const lowered = loweringTextmateGrammar(grammar)Note this function will not guarantee the correctness of the result, you many need to verify the result manually.
MIT License © 2024-PRESENT Anthony Fu