WCHawk

Q: Does Safari support JavaScript RegExp?

In a cross-browser test of RegExp, Safari seemed to not support it.

MacBook Pro, OS X Mavericks (10.9.1)

Posted on Sep 5, 2016 6:40 PM

Close

Q: Does Safari support JavaScript RegExp?

  • All replies
  • Helpful answers

  • by Carolyn Samit,

    Carolyn Samit Carolyn Samit Sep 5, 2016 8:37 PM in response to WCHawk
    Level 10 (119,429 points)
    Apple Watch
    Sep 5, 2016 8:37 PM in response to WCHawk

    No.

  • by VikingOSX,

    VikingOSX VikingOSX Sep 6, 2016 7:13 AM in response to WCHawk
    Level 7 (20,544 points)
    Mac OS X
    Sep 6, 2016 7:13 AM in response to WCHawk

    The answer is yes, Safari has supported JavaScript RegExp for years. It is straight forward when used in HTML. If you use RegExp in a Do JavaScript within AppleScript, it invokes an Apple Event, and Safari 9.1.3 will block you with the following dialog:

    Screen Shot 2016-09-06 at 9.30.47 AM.jpg

    One then enables Allow JavaScript from Apple Events from the Safari Develop menu, at the cost of a user password prompt when the AppleScript is run.

     

    Look at the Regular Expressions example for JavaScript at Rosetta Code. Add that to a <script> section in an HTML document, and add the following line before your closing </script> tag, before you open the HTML in Safari 9.1.3.

     

    alert(isMatch + " " + matches[1]);