Home > Mobile >  javascript regex for wss, ws http and https
javascript regex for wss, ws http and https

Time:01-31

I need to retrieve those URls from a string using regex in JavaScript

Here is an example string:

"Hello everyone! please join us at: https://oursite.com/. and 
please visit our courses websites: http://courseone.com.eu & http://coursetwo.us. 
For prod use websocket with this url: wss://localhost:4500/websocket/. and 
for staging use this url: ws://localhost:4500/websocket".

Now I want to extract these URls from the above string:

Like this:

https://oursite.com/
http://courseone.com.eu
http://coursetwo.us
wss://localhost:4500/websocket/
ws://localhost:4500/websocket

Now I followed this regex given in Detect URLs in text with JavaScript

/(https?:\/\/[^\s] )/g;

But it is not properly working for me since I have wss and ws URls aswell

Can anyone help me with the regex?

CodePudding user response:

Using this part [^\s] in your pattern matches too much.

Depending on the formats and the characters that you want to allow in the links, you can get the desired result from the question by matching optional non whitespace characters and then end on not a . or "

\b(?:http|ws)s?:\/\/\S*[^\s."]

Regex demo

const regex = /\b(?:http|ws)s?:\/\/\S*[^\s."]/g;
const s = `"Hello everyone! please join us at: https://oursite.com/. and 
please visit our courses websites: http://courseone.com.eu & http://coursetwo.us. 
    For prod use websocket with this url: wss://localhost:4500/websocket/. and 
    for staging use this url: ws://localhost:4500/websocket".`

console.log(s.match(regex));

Or end the match on a word character followed by an optional forward slash:

\b(?:http|ws)s?:\/\/\S*\w\/?

Regex demo

  • Related