@dpatt/delimiterized-regex-builder
Version:
Generate a regex from an array of strings to match all of the strings in order, separated by a delimiter.
85 lines (55 loc) • 2.33 kB
Markdown
A simple library that generates a regex from an array of strings, matching them in order and separated by a specified delimiter. This library was initially created to write tests that would ignore whitespace changes.
```bash
npm install @dpatt/delimiterized-regex-builder
```
```javascript
import { generateRegexFromArray, Delimiter } from '@dpatt/delimiterized-regex-builder';
const stringArray = ['apple', 'orange', 'banana'];
const regex = generateRegexFromArray(stringArray, Delimiter.whitespace);
const testString = 'apple orange banana';
const isMatch = regex.test(testString);
console.log(isMatch);
// Output: true
```
Generate a regex from an array of strings to match all of the strings, in order, separated by a specified delimiter.
```javascript
import { generateRegexFromArray, Delimiter } from '@dpatt/delimiterized-regex-builder';
const stringArray = ['apple', 'orange', 'banana'];
const regex = generateRegexFromArray(stringArray, Delimiter.whitespace);
const testString = 'apple orange banana';
const isMatch = regex.test(testString);
console.log(isMatch);
// Output: true
```
- `array`: An array of strings to generate the regex.
- `delimiter` (Optional): The delimiter to use between the strings. Defaults to `Delimiter.whitespace`.
An enum representing common delimiters for generating regex.
- `whitespace`: Represents one or more whitespace characters `[\\s\\n]+`.
- `wildcard`: Represents the wildcard characters `.*`.
Below is an example of how to use `generateRegexFromArray` in a Playwright test.
```javascript
import {
generateRegexFromArray,
Delimiter,
} from '@dpatt/delimiterized-regex-builder';
expect(locator).toHaveText(
generateRegexFromArray(
[],
Delimiter.whitespace
)
);
```
If you would like to add support for other delimiters, feel free to open a PR or create an Issue.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
---
Feel free to customize the README further based on any additional information you want to include or specific usage instructions you may have.