
Security News
GitHub Actions Pricing Whiplash: Self-Hosted Actions Billing Change Postponed
GitHub postponed a new billing model for self-hosted Actions after developer pushback, but moved forward with hosted runner price cuts on January 1.
regex-mapper
Advanced tools
A small utility library that provides functions for mapping and matching strings against a set of regular expressions.
A small utility library that provides functions for mapping and matching strings against a set of regular expressions.
First, ensure you have Node.js installed. Then, you can install this library using npm (or yarn):
npm install regex-mapper
or import by cdn https://cdn.jsdelivr.net/npm/regex-mapper/dist/index.umd.js
Certainly! Below is an example of how to use the regex-mapper library by importing it via CDN with the UMD (Universal Module Definition) format in an HTML file:
regexMapperCreates a function that maps a given string to a key, based on the regular expressions or strings provided in a map.
import { regexMapper } from './regex-utils';
const map = {
greeting: /^hello/,
farewell: /bye$/,
};
const match = regexMapper(map);
console.log(match("hello world")); // Output: 'greeting'
console.log(match("goodbye")); // Output: 'farewell'
console.log(match("unknown")); // Output: undefined
regexMapperStrictSimilar to regexMapper, but throws an error when no match is found.
import { regexMapperStrict } from './regex-utils';
const map = {
greeting: /^hello/,
farewell: /bye$/,
};
const matchStrict = regexMapperStrict(map);
console.log(matchStrict("hello world")); // Output: 'greeting'
console.log(matchStrict("goodbye")); // Output: 'farewell'
console.log(matchStrict("unknown")); // Throws an error
regexMatcherCreates a function that returns all keys whose corresponding regular expression matches the given string.
import { regexMatcher } from './regex-utils';
const map = {
greeting: /^hello/,
farewell: /bye$/,
casual: /yo/,
};
const matchAll = regexMatcher(map);
console.log(matchAll("hello yo")); // Output: ['greeting', 'casual']
console.log(matchAll("farewell bye")); // Output: ['farewell']
console.log(matchAll("unknown")); // Output: []
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Regex Mapper Example</title>
</head>
<body>
<h1>Regex Mapper Example</h1>
Check console
<script src="https://cdn.jsdelivr.net/npm/regex-mapper/dist/index.umd.js"></script>
<script>
const { mapStrings, matchStrings } = RegexMapper;
const map = {
greeting: /^hello/,
farewell: /bye$/,
};
const match = regexMapper(map);
console.log(match("hello world")); // Output: 'greeting'
console.log(match("goodbye")); // Output: 'farewell'
console.log(match("unknown")); // Output: undefined
// Example strings to match
const strings = ["hello123", "world", "123", "hello world", "test"];
// Use the mapStrings function to map each string to labels based on the regular expressions
const mappedStrings = mapStrings(strings, regexes);
// Use the matchStrings function to find matches for a single string
const singleMatches = matchStrings("hello123", regexes);
console.log("Mapped Strings:", mappedStrings);
console.log("Single Matches for 'hello123':", singleMatches);
</script>
</body>
</html>
To contribute or modify the library, follow these steps:
git clone https://github.com/snomiao/regex-mapper.git
cd regex-mapper
npm install
npm test
This project is licensed under the MIT License. See the LICENSE file for more details.
Feel free to open issues or submit pull requests. Contributions are welcome!
This utility was built using the error handling library DIE.
Happy coding! 🎉
2024 snomiao snomiao@gmail.com
FAQs
A small utility library that provides functions for mapping and matching strings against a set of regular expressions.
We found that regex-mapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub postponed a new billing model for self-hosted Actions after developer pushback, but moved forward with hosted runner price cuts on January 1.

Research
Destructive malware is rising across open source registries, using delays and kill switches to wipe code, break builds, and disrupt CI/CD.

Security News
Socket CTO Ahmad Nassri shares practical AI coding techniques, tools, and team workflows, plus what still feels noisy and why shipping remains human-led.