0

Trying to create a regex in which the string should not start with http(s)://, http(s)://www. Rest of the string can be anything.

I used this regeg but its return true if we have http://

^(http://www.|https://www.|http://|https://)?[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$

Another one I tried is

var re = new RegExp("(http|https|ftp)://");
var str = "http://xxxx.com";
var match = re.test(str);
console.log(match);

this one is also returning true.

Demo here

let re = /(http|https|ftp):///;
let url = 'xxxx.xxxx.xxxx'; // this is valid but test returns false
let url2 = 'https://www.xxzx.com/xxx.aspx'; // this should fail as there is https://www in url

console.log(re.test(url)); //
console.log(re.test(url2)); //

Is this possible with regex?

1
  • How do you think your regex should discard strings like you wanted? You will need to use negative lookahead in this case. Commented May 10, 2019 at 4:50

5 Answers 5

1

You need to use negative lookahead in your regex to discard strings starting with protocols like http or https or ftp. You can use this regex,

^(?!(?:ftp|https?):\/\/(www\.)?).+$

Regex Demo

JS Demo,

const arr = ['xxxx.xxxx.xxxx','ftp://www.xxzx.com/xxx.aspx','https://www.xxzx.com/xxx.aspx','http://xxxx.com','https://xxzx.com/xxx.aspx','http://www.xxxx.com']

arr.forEach(s => console.log(s + " --> " + /^(?!(?:ftp|https?):\/\/(www\.)?).+$/.test(s)))

Sign up to request clarification or add additional context in comments.

3 Comments

Can we also make www.xxxx.com return false? Thats if it starts with www. fail. I made some changes and it worked. Is this the correct method regex101.com/r/ACBgQ9/2
Your method will do reject strings starting with www but some refinements could be done. Let me see and update.
@phantomCoder: The precise regex you need to use is this ^(?!(?:(?:ftp|https?):\/\/|www\.)).+$
1

It's probably possible to do with regexes, but unless you have to use a regex, you should use the URL class:

let HTTP_URL = 'https://www.xxzx.com/xxx.aspx'
let HTTPS_URL = 'https://www.xxzx.com/xxx.aspx'
let FTP_URL = 'ftp://www.xxzx.com/xxx.aspx'
let GOOD_PROTOCOL = 'mysql://www.xxzx.com/xxx.aspx'
let GOOD_INPUT = '129.123.12.123'

function test_url(url) {
    let bad_protocols = ['http:', 'https:', 'ftp:']
  try {
        var parsed = new URL(url)
  } catch {
    return true
  }
  return (!bad_protocols.contains(parsed.protocol))
}

test_url(HTTP_URL) //false
test_url(HTTPS_URL) //false
test_url(FTP_URL) //false
test_url(GOOD_PROTOCOL) //true
test_url(GOOD_INPUT) //true

3 Comments

Thanks for this, but I am looking for a regexp if possible since I am using Yup()
@phantomCoder you can use this with Yup using the .test() validator (medium.com/@arkadyt/…)
Thanks for the link, I prefer a regex solution as URL class has issues with IE. +1
0

If you're just trying to negate that regex:

function doesMatch(string) {
	return !/^http(s):\/\/(?:www)?/.test(string);
}

[
	'https://www.xxzx.com/xxx.aspx',
	'http://www.xxxx.com',
	'https://xxxx.com',
	'http://xxxx.com',
	'https://aaaa.com',
	'aaaa.com'
].forEach(s => console.log(doesMatch(s)));

Comments

0

In your example code, re.test(url) returns false , because there is no presence of http or https in that string. In url2 (ie..'https://www.xxzx.com/xxx.aspx') , there is a presence of https so it is returning true.

Comments

0

This expression might also work, it would allow your desired input and fails all other URLs, and you can also simply add to its char list, what else might be undesired to start:

^([^http|s|ftp|www|\/\/|])*

Pass

xxxx.xxxx.xxxx

Fail

ftp://www.xxzx.com/xxx.aspx
https://www.xxzx.com/xxx.aspx
http://xxxx.com
https://xxzx.com/xxx.aspx
http://www.xxxx.com
//www.xxxx.com

You can test/modify/change it in this link.

RegEx Descriptive Graph

This graph shows how the expression would work and you can visualize other expressions in this link:

enter image description here

Performance Test

This JavaScript snippet shows the performance of that expression using a simple 1-million times for loop.

const repeat = 1000000;
const start = Date.now();

for (var i = repeat; i >= 0; i--) {
	const string = 'xxxx.xxxx.xxxx';
	const regex = /(^([^http|s|ftp|www|\/\/|])*)/gm;
	var match = string.replace(regex, "$1");
}

const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.