William Robertson William Robertson - 1 year ago 62
Javascript Question

Regex to match quotes like q'[Mike's bike]'

I'm trying to write a regex that will match Oracle q-quotes for a PL/SQL lexer in code-prettify.js. For example,

q'[Here's Johnny]'

This should be matched the same as

'Here''s Johnny'

(that is, so that SQL will treat it all as one text string. The advantage of q-quotes over the conventional two-single-quotes is you don't have to go through your text string doubling up all of your single quotes.)

The quote delimiter can be any of [, {, <, or (, but I think if I can get it working with one bracket type then I can repeat the variations as ORs, like


Ultimately I want a single regex that will match an ordinary single-quoted string or a q-quote with any of the bracket types.

Answer Source

For your lexer you would like the text of q'[Here's Johnny]' and 'Here''s Johnny' to be matched. Assuming that you want the match to include all characters comprising the string token, including quotes, brackets, etc, this regular expression should work:


The two relevant pieces are:

  1. q'\[.*?\](?=')' is the basis for q-quoted strings, and the rest of the appropriate brackets have their own statements, and
  2. '(?:[^']|'')*', matches single-quoted strings.

You can see matching examples here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download