Determines if a given value is a regular expression.
Some usage examples for the isRegex
function:
import isRegex from './isRegex';
const pattern = /abc/;
console.log(isRegex(pattern)); // true
const nonPattern = 'abc';
console.log(isRegex(nonPattern)); // false
const maybePattern = '/[a-z]/';
if (isRegex(maybePattern)) {
console.log(maybePattern.test('hello')); // Executes safely
} else {
console.log('Not a valid regex pattern');
}
const items = [/test/, 'hello', /abc/, 123];
const regexItems = items.filter(isRegex);
console.log(regexItems); // [/test/, /abc/]
Your support inspires & encourage us more. If you are interested to make a donation to us, please click the below PayPal button.
This project is licensed under the MIT License.