-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathregex.ts
37 lines (32 loc) · 1.57 KB
/
regex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import regexLookbehindAvailable from "./utils/regexLookbehindAvailable";
const RESTRICTED_SYMBOLS = "☑️✓✔✅";
// We only want to match mention when the `@` character is at the start of the
// line or right after a whilespace.
const MATCH_BEHIND = regexLookbehindAvailable ? "(?<=^|\\s)" : "";
const MENTION_NAMESPACE = "\\w+\\/";
const MENTION_BODY = "([\\dA-Za-z]\\w{2,25})";
const EDITOR_MENTION = "([\\dA-Za-z]\\w*)"; // This will start searching for mentions after the first character
const EDITOR_GROUP = "([\\dA-Za-z]\\w*)"; // This will start searching for groups after the first character
export const Regex = {
cashtag: /(\$\w*[A-Za-z]\w*)/g,
evmAddress: /^(0x)?[\da-f]{40}$/i,
username: /^[\dA-Za-z]\w{2,25}$/g,
hashtag: /(#\w*[A-Za-z]\w*)/g,
// Match string like @lens/someone.
mention: new RegExp(
`${MATCH_BEHIND}@${MENTION_NAMESPACE}${MENTION_BODY}`,
"g"
),
// Match string like @someone.
accountNameFilter: new RegExp(`[${RESTRICTED_SYMBOLS}]`, "gu"),
accountNameValidator: new RegExp(`^[^${RESTRICTED_SYMBOLS}]+$`),
txHash: /^0x[\dA-Fa-f]{64}$/,
// modified version of https://stackoverflow.com/a/6041965/961254 to support unicode international characters
url: /\b(http|https):\/\/([\p{L}\p{N}_-]+(?:(?:\.[\p{L}\p{N}_-]+)+))([\p{L}\p{N}_.,@?^=%&:\/~+#-]*[\p{L}\p{N}_@?^=%&\/~+#-])/gu
};
export const EditorRegex = {
// group looks like: /bonsai or /gho
group: new RegExp(`${MATCH_BEHIND}\/(${EDITOR_GROUP})$`, "g"),
emoji: new RegExp(`${MATCH_BEHIND}:\\w*$`, "g"),
mention: new RegExp(`${MATCH_BEHIND}@${EDITOR_MENTION}$`, "g")
};