https://github.com/devxprite/value-extractor
A simple JavaScript library for extracting values from string based on a pattern
https://github.com/devxprite/value-extractor
npm npm-package
Last synced: about 1 month ago
JSON representation
A simple JavaScript library for extracting values from string based on a pattern
- Host: GitHub
- URL: https://github.com/devxprite/value-extractor
- Owner: devxprite
- License: mit
- Created: 2023-01-04T08:27:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-02-07T11:19:45.000Z (over 2 years ago)
- Last Synced: 2025-03-31T12:58:08.869Z (3 months ago)
- Topics: npm, npm-package
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/value-extractor
- Size: 50.8 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
## value-extractor
This is a simple and easy-to-use JavaScript library that allows you to extract values from a string based on a pattern. It is useful when you have a string with a predictable structure and you want to extract specific values from it.
### Use in Browser
Use directly in the browser with [jsDelivr](https://www.jsdelivr.com/package/npm/value-extractor) or [unpkg](https://unpkg.com/value-extractor/).
```html
const str = "change 100 kg to pounds";
const pattern = "change {quantity} {from_unit} to {to_unit}";
const values = valueExtractor(str, pattern);
console.log(values)
// OUTPUT :
// {
// quantity: "100",
// from_unit: "kg",
// to_unit: "pounds"
// }
```
### Install as a NPM package
To install `value-extractor` package, run the following command:
```bash
npm i value-extractor
```
### Usage
To use the String Value Extractor, import the extractValues function and call it with a string and a pattern:```javascript
import valueExtractor from 'value-extractor';const values = valueExtractor('/2012/08/12/test.html', '/{year}/{month}/{day}/{title}.html');
console.log(values);
// Output: { "year": "2012", "month": "08", "day": "12", "title": "test" }
```
The `extractValues` function returns an object with keys based on the pattern, and values based on the extracted values.