0
$regex = "_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS";

Where do i added my Expression [A-Za-z0-9.,/&=?:_+#%-] to allow only these specific characters to be used while keeping the form of the URL as it is made in $regex,

I've tried adding it at the beginning after _^ but it finds every URL an invalid url, That after removing the _ from the Expression too.

The $regex works ok and prevents weird urls like www..doma@$#n.#@om but when it comes to the part after that like

www.domain.com/<script>alert('Hacked')</script>, It just allows every single special character to be used, So i've decided to allow only Alphanumeric and . , / & = ? : _ + # % - since they're used in every URL.

1 Answer 1

1

Just replace [^\s] with the expression you want [A-Za-z0-9.,/&=?:_+#%-]

Example: https://regex101.com/r/kWB1q1/2

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

3 Comments

I've noticed that _ and _iuS was removed, What was the usage of them if the RegEx works without them?
The _ are used in PHP as delimiters in your case. For more information about delimiters read pl.php.net/manual/en/regexp.reference.delimiters.php
iuS are your flags, if you want to know what flags there are and what they stand for, click on the flag icon @ regex101..

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.