T
T
TheSerKat2021-02-03 11:41:58
HTML
TheSerKat, 2021-02-03 11:41:58

How to match https://site.com and site.com links in react app?

2 links come from the database
url1 = ' https://facebook.com '
url2 = 'github.com'

<a href={props.url1}>Facebook</a> //Переходит на сайт как абсолютная ссылка
<a href={props.url2}>Github</a> //Переходит  localhost/github.com как относительная ссылка

How can I make them all absolute links, regardless of whether the link contains https:// ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
OxCom, 2021-02-03
@TheSerKat

before displaying the link, you can check for the presence of schema and if not, do your magic.

console.log(/https?:\/\/.*?/.exec('https://github.com'));
// ["https://", index: 0, input: "https://github.com", groups: undefined]

console.log(/https?:\/\/.*?/.exec('github.com'));
// null

S
smigles, 2021-02-03
@smigles

Those without https://must be with http://.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question