Repository navigation
Releases: MaestroError/simplified-regex
Release list
New Features: Replace, Search & Swap
This release includes 4 new features (action methods) and implemented "named groups" support with namedGroup method. Some fixes was applied as well. For detailed changes see Changelog at bottom of the release note, but now let's discuss the main features added:
Replace Method
Replaces found matches in the given source string using the provided callback.
const result = new RegexBuilder()
.start("This is a #test")
.hash()
.text()
.replace((foundItem) => {
return `<a href='${foundItem}'>${foundItem}</a>`;
});Returns: "This is a <a href='#test'>#test</a>"
Check docs for more examples.
Search & SearchReverse Methods
searches for a keyword or pattern (including ready-to-use patterns) in multiline text and returns lines where the subject is found. SearchReverse does the same, but returns lines where subject couldn't be found. It is especially useful for processing large files like logs or JSON.
For example:
// Find all log types except INFO
const result = new RegexBuilder()
.source(
`
[2024-12-23 10:00:00] INFO: User logged in.
[2024-12-25 10:05:00] ERROR: Unable to connect to database.
[2024-12-25 10:10:00] INFO: User updated profile.
[2024-12-15 10:15:00] WARNING: Disk space running low.
[2024-12-34 10:20:00] ERROR: Timeout while fetching data.
`
)
.searchReverse("INFO");
/* Returns: [
'[2024-12-25 10:05:00] ERROR: Unable to connect to database.',
'[2024-12-15 10:15:00] WARNING: Disk space running low.',
'[2024-12-34 10:20:00] ERROR: Timeout while fetching data.'
] */Check docs for more examples.
Swap method
Allows you to swap any kind of data logically, for example, build new URLs from old ones. It utilizes the "named groups" regex feature and can be used with a callback or pattern string.
const builder = new RegexBuilder()
.start(
"URIs: /container-tbilisi-1585, /container-berlin-1234, /container-tbilisi-2555"
)
.slash() // "/"
.exact("container") // "container" (static part of URI)
.dash() // "-"
.namedGroup((pattern) => pattern.text(), "City") // Text between dashes, Grouped & named as "City"
.dash() // "-"
.namedGroup((pattern) => pattern.digitsRange(2, 5), "id") // Numbers at end, Grouped & named as "id"
.end(); // Ends custom pattern to make "swap" method available
// Using swap with pattern string
// which will swap placeholders like "[ID]" with
// Extracted data for each found match
const result = builder.swap("/container/[ID]?city=[CITY]");
/* Returns:
[
'/container/1585?city=tbilisi',
'/container/1234?city=berlin',
'/container/2555?city=tbilisi'
]
*/Check docs for more examples.
Named capturing groups
used to group part of a pattern together and capture the matching text for later use with its name. Note that it returns an array with a different structure when using retrieval methods like get
// Matching a date format with capturing the parts as separated groups
const result = new RegexBuilder()
.start("RI-2142, PO-2555")
.namedGroup(
(pattern) => {
return pattern.textUppercase(2);
},
"project",
1
)
.dash()
.namedGroup(
(pattern) => {
return pattern.digitsRange(2, 4);
},
"issue",
1
)
.get();
/* Returns:
[
{
"result": "RI-2142",
"groups": {
"project": "RI",
"issue": "2142",
}
},
{
"result": "PO-2555",
"groups": {
"project": "PO",
"issue": "2555",
}
}
]
*/What's Changed
- Replace, search, searchReverse & swap methods, support for named grou… by @MaestroError in #1
Full Changelog: 1.0.3...1.1.1
Fixes for NPM
Updated Docs
Initial release
1.0.0 Github action, package.json update and basic docs