-2

The provided string is javascript RegExp or not

Is there any kind of method that will check the provided string is javascript RegExp format or not?

adiga
  • 34,372
  • 9
  • 61
  • 83
UDIT SONI
  • 81
  • 1
  • 10
  • 3
    Possible duplicate of [Is there a regular expression to detect a valid regular expression?](https://stackoverflow.com/questions/172303/is-there-a-regular-expression-to-detect-a-valid-regular-expression) – adiga May 08 '19 at 12:36

1 Answers1

0

You can try below code

var isRegExp = true;
try {
    new RegExp("regexphere");
} catch(e) {
    isRegExp = false;
}

if(!isRegExp) alert("Not a valid regular expression");
Alex
  • 878
  • 1
  • 10
  • 25