DD
DevDash

Regex for WebSocket URL

Regex Pattern

^wss?:\/\/[^\s/$.?#].[^\s]*$

WebSocket URL starting with ws:// or wss://

Quick Answer

The regex pattern for websocket url is `^wss?:\/\/[^\s/$.?#].[^\s]*$`. WebSocket URL starting with ws:// or wss://. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
ws://example.com/socket✓ Matches
wss://secure.example.com:443/ws✓ Matches
ws://localhost:8080✓ Matches
http://example.com✗ No match
ws://✗ No match
example.com/ws✗ No match

Code Examples

javascript

const regex = /^wss?:\\/\\/[^\s\/$.?#].[^\s]*$/;
const isValid = regex.test(value);

python

import re
pattern = r'^wss?:\/\/[^\s/$.?#].[^\s]*$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^wss?:\/\/[^\s/$.?#].[^\s]*$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^wss?:\\/\\/[^\s\/$.?#].[^\s]*$/', $value)) {
    echo "valid";
}

java

String pattern = "^wss?:\\/\\/[^\\s/$.?#].[^\\s]*$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.