diff --git a/src/Str.php b/src/Str.php index 4961ea5..576fa47 100644 --- a/src/Str.php +++ b/src/Str.php @@ -622,4 +622,15 @@ public static function cleanWhitespace(string $str, string $charlist = " \t\n\r\ { return trim(preg_replace('/[\t\n\r\s]+/', ' ', $str), $charlist); } + + /** + * Tests a string as a Regular Expression (regex) + * @return bool true if valid. + */ + public static function isValidRegex(string $regex): bool + { + @preg_match($regex, ''); + + return preg_last_error() === PREG_NO_ERROR; + } }