JavaScript in Zotero 6: Regex, Unicode and Look behind

edited December 22, 2022
Apparently, while running JavaScript in Zotero 6, regular expressions do not support:
- lookbehinds (/(?<=…)…/),
- unicode sets (e.g., /p{Lu}/u).

I'm trying to code a plugin for Zotero 6, and I have a few regular expressions in my code.
Is there any solutions other than rewriting my code (which can be a hassle in a few cases)?

(Edit: The post originally also wrongfully listed lookaheads.)
  • I haven't tested, but are you sure lookaheads aren't supported. They should be standard?
    lookbehinds and unicode sets aren't supported in the Firefox version on which Zotero is built (I think 68?)
    (see https://caniuse.com/mdn-javascript_builtins_regexp_property_escapes
    and https://caniuse.com/js-regexp-lookbehind ). Not much you can do about that I think.
    I'd expect them to work in the next major version (Zotero 7) which is currently built on FF108.
  • Pretty sure yes. Typically, this:
    const test = ': Put some smiles : : :'.replace( /(?<=:)/g, ')' );
    test

    should give me: :) Put some smiles :) :) :)

    Yet, using it in Zotero's “Run JavaScript” window gives:
    SyntaxError: invalid regexp group

    (?. and ?? operators also aren't supported. Although they are easier to replace.)

    Yeah I expect these issues to be gone on Zotero 7, so I'll keep trace of clean implementation… But in the meanwhile, I'd like to have my stuff run on Zotero 6.
  • That's the lookbehind, though, not the lookahead.
    The lookahead works as expected:

    const test = ': Put some frowns : : :'.replace( /(?=:)/g, ')' );
    test


    --> ): Put some frowns ): ): ):
  • Indeed. I was actually expecting both of them to be either working or not at the same time. You're right, lookaheads, specifically, do work.
    (Saved me one modification. I mostly have lookbehinds.)
  • (Lookaheads are base regex; lookbehinds ECMA 2018 I believe)
Sign In or Register to comment.