From 079469f6652f6daac7039df9cce1a73195683511 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 20 Sep 2023 12:10:49 -0500 Subject: [PATCH] Remove useless escape characters from regexes --- src/features/compose/util/counter.ts | 2 +- src/features/compose/util/url-regex.ts | 11 ++++++----- src/normalizers/instance.ts | 2 +- src/rtl.ts | 4 ++-- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/features/compose/util/counter.ts b/src/features/compose/util/counter.ts index 379da63b9..6d999a547 100644 --- a/src/features/compose/util/counter.ts +++ b/src/features/compose/util/counter.ts @@ -5,5 +5,5 @@ const urlPlaceholder = 'xxxxxxxxxxxxxxxxxxxxxxx'; export function countableText(inputText: string) { return inputText .replace(urlRegex, urlPlaceholder) - .replace(/(^|[^\/\w])@(([a-z0-9_]+)@[a-z0-9\.\-]+[a-z0-9]+)/ig, '$1@$3'); + .replace(/(^|[^/\w])@(([a-z0-9_]+)@[a-z0-9.-]+[a-z0-9]+)/ig, '$1@$3'); } diff --git a/src/features/compose/util/url-regex.ts b/src/features/compose/util/url-regex.ts index 5b015240e..d5171ac87 100644 --- a/src/features/compose/util/url-regex.ts +++ b/src/features/compose/util/url-regex.ts @@ -32,7 +32,7 @@ const stringSupplant = function(str: string, values: { [x: string]: any }) { export const urlRegex = (function() { regexen.spaces_group = /\x09-\x0D\x20\x85\xA0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000/; // eslint-disable-line no-control-regex regexen.invalid_chars_group = /\uFFFE\uFEFF\uFFFF\u202A-\u202E/; - regexen.punct = /\!'#%&'\(\)*\+,\\\-\.\/:;<=>\?@\[\]\^_{|}~\$/; + regexen.punct = /!'#%&'\(\)*\+,\\-\.\/:;<=>\?@\[\]\^_{|}~\$/; regexen.validUrlPrecedingChars = regexSupplant(/(?:[^A-Za-z0-9@@$###{invalid_chars_group}]|^)/); regexen.invalidDomainChars = stringSupplant('#{punct}#{spaces_group}#{invalid_chars_group}', regexen); regexen.validDomainChars = regexSupplant(/[^#{invalidDomainChars}]/); @@ -148,7 +148,7 @@ export const urlRegex = (function() { regexen.validDomain = regexSupplant(/(?:#{validSubdomain}*#{validDomainName}(?:#{validGTLD}|#{validCCTLD}|#{validPunycode}))/); regexen.validPortNumber = /[0-9]+/; regexen.pd = /\u002d\u058a\u05be\u1400\u1806\u2010-\u2015\u2e17\u2e1a\u2e3a\u2e40\u301c\u3030\u30a0\ufe31\ufe58\ufe63\uff0d/; - regexen.validGeneralUrlPathChars = regexSupplant(/[^#{spaces_group}\(\)\?]/i); + regexen.validGeneralUrlPathChars = regexSupplant(/[^#{spaces_group}()?]/i); // Allow URL paths to contain up to two nested levels of balanced parens // 1. Used in Wikipedia URLs like /Primer_(film) // 2. Used in IIS sessions like /S(dfd346)/ @@ -171,17 +171,18 @@ export const urlRegex = (function() { 'i'); // Valid end-of-path characters (so /foo. does not gobble the period). // 1. Allow =&# for empty URL parameters and other URL-join artifacts - regexen.validUrlPathEndingChars = regexSupplant(/[^#{spaces_group}\(\)\?!\*';:=\,\.\$%\[\]#{pd}~&\|@]|(?:#{validUrlBalancedParens})/i); + regexen.validUrlPathEndingChars = regexSupplant(/[^#{spaces_group}()?!*';:=,.$%[\]#{pd}~&|@]|(?:#{validUrlBalancedParens})/i); // Allow @ in a url, but only in the middle. Catch things like http://example.com/@user/ regexen.validUrlPath = regexSupplant('(?:' + '(?:' + '#{validGeneralUrlPathChars}*' + '(?:#{validUrlBalancedParens}#{validGeneralUrlPathChars}*)*' + '#{validUrlPathEndingChars}' + + // eslint-disable-next-line no-useless-escape ')|(?:@#{validGeneralUrlPathChars}+\/)' + ')', 'i'); - regexen.validUrlQueryChars = /[a-z0-9!?\*'@\(\);:&=\+\$\/%#\[\]\-_\.,~|]/i; - regexen.validUrlQueryEndingChars = /[a-z0-9_&=#\/]/i; + regexen.validUrlQueryChars = /[a-z0-9!?*'@();:&=+$/%#[\]\-_.,~|]/i; + regexen.validUrlQueryEndingChars = /[a-z0-9_&=#/]/i; regexen.validUrl = regexSupplant( '(' + // $1 URL '(https?:\\/\\/)' + // $2 Protocol diff --git a/src/normalizers/instance.ts b/src/normalizers/instance.ts index b198c7b6a..ea3327fb6 100644 --- a/src/normalizers/instance.ts +++ b/src/normalizers/instance.ts @@ -102,7 +102,7 @@ const getAttachmentLimit = (software: string | null) => software === PLEROMA ? I const normalizeVersion = (instance: ImmutableMap) => { return instance.update('version', '0.0.0', version => { // Handle Mastodon release candidates - if (new RegExp(/[0-9\.]+rc[0-9]+/g).test(version)) { + if (new RegExp(/[0-9.]+rc[0-9]+/g).test(version)) { return version.split('rc').join('-rc'); } else { return version; diff --git a/src/rtl.ts b/src/rtl.ts index 16a6100ef..2f165bda4 100644 --- a/src/rtl.ts +++ b/src/rtl.ts @@ -25,8 +25,8 @@ export function isRtl(text: string): boolean { text = text.replace(/(mailto:)([^\s@]+@[^\s@]+\.[^\s@]+)/g, ''); // Remove Phone numbe links text = text.replace(/(tel:)([+\d\s()-]+)/g, ''); - text = text.replace(/(?:^|[^\/\w])@([a-z0-9_]+(@[a-z0-9\.\-]+)?)/ig, ''); - text = text.replace(/(?:^|[^\/\w])#([\S]+)/ig, ''); + text = text.replace(/(?:^|[^/\w])@([a-z0-9_]+(@[a-z0-9.-]+)?)/ig, ''); + text = text.replace(/(?:^|[^/\w])#([\S]+)/ig, ''); text = text.replace(/\s+/g, ''); const matches = text.match(rtlChars);