Install via yarn add locutus and import:
import { sub } from 'locutus/python/re/sub'.
Or with CommonJS: const { sub } = require('locutus/python/re/sub')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
sub('a+', '-', 'caaab')
'c-b'
2
sub('(\\d+)', '#', 'a1b22c333')
'a#b#c#'
3
sub('x', 'y', 'xxx', 2)
'yyx'
4
sub('abc', 'X', 'ABC abc', 0, 2)
'X X'
Notes
Replaces regex matches in a string, similar to Python’s re.sub.
Supports count-limited substitution and a subset of numeric flags (IGNORECASE=2, MULTILINE=8, DOTALL=16).
Here's what our current TypeScript equivalent to Python's re.sub looks like.
let out = '' if (flags & PY_RE_IGNORECASE) { out += 'i' } if (flags & PY_RE_MULTILINE) { out += 'm' } if (flags & PY_RE_DOTALL) { out += 's' } return out }
functiondedupeFlags(flags: string): string { const seen = newSet<string>() let out = '' for (const flag of flags) { if (!seen.has(flag)) { seen.add(flag) out += flag } } return out }
functionapplyPythonReplacementTemplate( template: string, full: string, groups: Array<string | undefined>, named?: Record<string, string>, ): string { let out = ''
for (let i = 0; i < template.length; i++) { const char = template[i] if (char !== '\\') { out += char continue }
const next = template[i + 1] if (!next) { out += '\\' continue }
if (/\d/.test(next)) { let token = next let j = i + 2 while (j < template.length && /\d/.test(template[j] ?? '') && token.length < 2) { token += template[j] j += 1 } out += groups[Number.parseInt(token, 10) - 1] ?? '' i = j - 1 continue }
if (next === 'g' && template[i + 2] === '<') { const close = template.indexOf('>', i + 3) if (close > -1) { const token = template.slice(i + 3, close) if (/^\d+$/.test(token)) { out += groups[Number.parseInt(token, 10) - 1] ?? '' } else { out += named?.[token] ?? '' } i = close continue } }
let out = '' if (flags & PY_RE_IGNORECASE) { out += 'i' } if (flags & PY_RE_MULTILINE) { out += 'm' } if (flags & PY_RE_DOTALL) { out += 's' } return out }
functiondedupeFlags(flags) { const seen = newSet() let out = '' for (const flag of flags) { if (!seen.has(flag)) { seen.add(flag) out += flag } } return out }
functionapplyPythonReplacementTemplate(template, full, groups, named) { let out = ''
for (let i = 0; i < template.length; i++) { const char = template[i] if (char !== '\\') { out += char continue }
const next = template[i + 1] if (!next) { out += '\\' continue }
if (/\d/.test(next)) { let token = next let j = i + 2 while (j < template.length && /\d/.test(template[j] ?? '') && token.length < 2) { token += template[j] j += 1 } out += groups[Number.parseInt(token, 10) - 1] ?? '' i = j - 1 continue }
if (next === 'g' && template[i + 2] === '<') { const close = template.indexOf('>', i + 3) if (close > -1) { const token = template.slice(i + 3, close) if (/^\d+$/.test(token)) { out += groups[Number.parseInt(token, 10) - 1] ?? '' } else { out += named?.[token] ?? '' } i = close continue } }
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.