JavaScript in Zotero 6: Regex, Unicode and Look behind
Apparently, while running JavaScript in Zotero 6, regular expressions do not support:
- lookbehinds (
- unicode sets (e.g.,
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.)
- 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.)
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.
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.
The lookahead works as expected:
const test = ': Put some frowns : : :'.replace( /(?=:)/g, ')' );
test
--> ): Put some frowns ): ): ):
(Saved me one modification. I mostly have lookbehinds.)