diff --git a/src/containers/shared/components/DomainLink.tsx b/src/containers/shared/components/DomainLink.tsx index 1d6231e13..a43600595 100644 --- a/src/containers/shared/components/DomainLink.tsx +++ b/src/containers/shared/components/DomainLink.tsx @@ -7,16 +7,19 @@ export interface Props { domain: string } +// Matches a protocol (e.g. 'http://' or 'https://') at the start of a string. +const protocolRegex = /^([a-z][a-z0-9+\-.]*):\/\// + const DomainLink = (props: Props) => { const { className, decode = false, domain } = props // If decode is true, decode the domain const decodedDomain = decode ? decodeHex(domain) : domain - // Check if the decodedDomain contains a protocol (http:// or https://) - const domainHasProtocol = decodedDomain.match(/^([a-z][a-z0-9+\-.]*):\/\//) + // Use the test method to check for the protocol + const domainHasProtocol = protocolRegex.test(decodedDomain) - // if the decodedDomain already has a protocol, use it; oitherwise, add 'https://' + // If decoded domain does not have a protocol, add one ; otherwise, don't const href = domainHasProtocol ? decodedDomain : `https://${decodedDomain}` return (