-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathregex.ts
More file actions
21 lines (19 loc) · 979 Bytes
/
regex.ts
File metadata and controls
21 lines (19 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'use strict';
const NUMBER_REGEX = '([2-9]|[1-9][0-9]+)?';
/**
* make a regex for matching counter ids/names ie xaxis, xaxis2, xaxis10...
*
* @param head: the head of the pattern, eg 'x' matches 'x', 'x2', 'x10' etc.
* 'xy' is a special case for cartesian subplots: it matches 'x2y3' etc
* @param tail: a fixed piece after the id
* eg counterRegex('scene', '.annotations') for scene2.annotations etc.
* @param openEnded: if true, the string may continue past the match.
* @param matchBeginning: if false, the string may start before the match.
*/
export function counter(head: string, tail: string = '', openEnded: boolean, matchBeginning: boolean) {
const fullTail = tail + (openEnded ? '' : '$');
const startWithPrefix = matchBeginning === false ? '' : '^';
return head === 'xy'
? new RegExp(startWithPrefix + 'x' + NUMBER_REGEX + 'y' + NUMBER_REGEX + fullTail)
: new RegExp(startWithPrefix + head + NUMBER_REGEX + fullTail);
}