How Can I Validate a URL end point in Javascript? - javascript

I want to validate the url end point using regex. Example end point like: /user/update.
First I tried with (/[A-Za-z0-9_.:-~/]*) but also matches http://url.com/user/update with javascript regex. I want the string to only validate pass if it is equal to /user/update like end points

You can use regex look behind technique to get the path after the .com with /(?<=.com).*/
const matchEndPoint = (str) => str.match(/(?<=.com).*/)
const [result] = matchEndPoint('http://url.com/user/update');
console.log(result)

You might use a pattern like
^\/[\w.:~-]+\/[\w.:~-]+$
Regex demo
Or for example not allowing consecutive dashes like -- and match one or more forward slashes:
^\/\w+(?:[.:~-]\w+)*(?:\/\w+(?:[.:~-]\w+)*)*$
Explanation
^ Start of string
\/\w+ Match / and 1+ word chars
(?:[.:~-]\w+)* Optionally repeat a char of the character class and 1+ word chars
(?: Non capture group
\/\w+ Match / and 1+ word chars
(?:[.:~-]\w+)* Optionally repeat a char of the character class and 1+ word chars
)* Close group and optionally repeat
$ End of string
Regex demo

Related

Regex to get last part of url without appended version and parameters

Hi guys I've got a very specific request where I would like to get the last part of a url without the parameters but if the name of the script has a version appended, like -V2, where the 2 could be any number, the regex would ignore it.
So far I found this (?!\/)(\w+)(?=.js) but it is only getting a single word.
Some examples:
https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2.js?x=123&name=bo-b
https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js?x=123&name=bo-b
https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js
https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-v2.js
All should match sampleScript
/\/((.(?!\/))+?)(-v\d|)\.js/i
\/ matches the character /
1st Capturing Group ((.(?!\/))+?)
2nd Capturing Group (.(?!\/))+?
. matches any character
+? matches the previous token between one and unlimited times, as
few times as possible, expanding as needed (lazy)
Negative Lookahead (?!\/) Assert that the Regex below does not match : \/ matches the character /
3rd Capturing Group (-v\d|)
1st Alternative
-v matches the characters -v
\d matches a digit (equivalent to [0-9])
2nd Alternative
null, matches any position
\. matches the character .
js matches the characters js
Global pattern flags i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
const urls = [
'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2.js?x=123&name=bo-b',
'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js?x=123&name=bo-b',
'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js',
'https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-v2.js'
];
const regexp = /\/((.(?!\/))+?)(-v\d|)\.js/i;
urls.forEach(url => console.log(regexp.exec(url)[1]));
You might use:
.*\/((?:(?!-[Vv]\d+\b)[^\s\/])*)\.js\b
Explanation
.*\/ Match till the last occurrence of /
( capture group 1
(?: Non capture group
(?!-[Vv]\d+\b) Assert not -v followed by digits to the right
[^\s\/] Match any non whitespace char except /
)* Close non capture group and optionally repeat
) Close group 1
\.js\b Match .js followed by a word boundary
Regex demo
const regex = /.*\/((?:(?!-[Vv]\d+\b)[^\s\/])*)\.js\b/;
[
"https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-V2.js?x=123&name=bo-b",
"https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js?x=123&name=bo-b",
"https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript.js",
"https://s3.amazon-aws.com/bob.success.com/scripts/sampleScript-v2.js",
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(m[1]);
}
})
Another option with a lookahead only:
.*\/((?!\w*-[vV]\d+\b)[^\s\/]*)\.js\b
Regex demo
To match just "sampleScript" in all examples, using a lookahead to match ".js" optionally preceded by "-v" or "-V" and a digit: (?<=\/)[^/]+(?=(?:-[vV]\d)?\.js).
To match the entire file name and extension, just remove the lookahead: (?<=\/)[^/]+(?:-[vV]\d)?\.js.
(If the regex can have the i flag, you can use v instead of [vV].)

typescript Yup validation or condition with regex using matches problem

I'm trying to make sure only one type of special character (semi-colon, comma, or space) is used in a string.
Valid case:
Can contain alphanumeric letters and numbers
Can contain special characters : / .
Can contain only one type of special characters from: space, semi-colon or comma in the string
e.g this should match as it only uses one type of special character (semi-colon):
https://hello.com/example1;https://hello.com.com/example2;https://hello.com.com/example3
This should fail as it mixes two types of special characters (space and semi-colon)
https://hello.com/example1; https://hello.com.com/example2 ;https://hello.com.com/example3
This is my code:
const myValidation = yup
.string()
.matches(/^([A-Za-z0-9://,\\.]|[A-Za-z0-9:// \\.]|[A-Za-z0-9://;\\.])+$/, 'Please separate each with a comma, space or semicolon')
.required();
When i only have /^([A-Za-z0-9://,\\.]+$/ it works correctly to only match the string if it has a only a comma as special character:
https://hello.com/example1,https://hello.com.com/example2,https://hello.com.com/example3
but as soon as i add the other or conditions /^([A-Za-z0-9://,\\.]|[A-Za-z0-9:// \\.]|[A-Za-z0-9://;\\.])+$/ it starts allowing for semi-colon and space and comma special characters in the string at the same time (the invalid case)
For the valid cases, you can use a capture group with a backreference \1 to make sure that the "special character" is the same delimiter between the matches
^[A-Za-z0-9:/.]+(?:([ ,;])[A-Za-z0-9:/.]+(?:\1[A-Za-z0-9:/.]+)*)?$
The pattern matches:
^ Start of string
[A-Za-z0-9:/.]+ Match 1+ of the allowed characters
(?: Non capture group to match as a whole part
([ ,;]) Capture group 1, match one of the delimiters
[A-Za-z0-9:/.]+ Match 1+ of the allowed characters
(?:\1[A-Za-z0-9:/.]+)* Optionally repeat a backreference to the same delimiter and again 1+ of the allowed characters
)? Close the non capture group and make it optional
$ End of string
See a regex demo.
const regex = /^[A-Za-z0-9:/.]+(?:([ ,;])[A-Za-z0-9:/.]+(?:\1[A-Za-z0-9:/.]+)*)?$/;
[
"https://hello.com/example1;https://hello.com.com/example2;https://hello.com.com/example3",
"https://hello.com/example1; https://hello.com.com/example2 ;https://hello.com.com/example3",
"https://hello.com/example1"
].forEach(s =>
console.log(`${regex.test(s)} --> ${s}`)
);
If there should be at least a single delimiter present, you could shorten the pattern to:
^[A-Za-z0-9:/.]+([ ,;])[A-Za-z0-9:/.]+(?:\1[A-Za-z0-9:/.]+)*$
If the strings should start with http:// or https:// you could use:
^https?:\/\/[A-Za-z0-9:/.]+(?:([ ,;])https?:\/\/[A-Za-z0-9:/.]+(?:\1[A-Za-z0-9:/.]+)*)?$
See another regex demo.
Is only one
function isOnlyOne() {
let s = "zy,,aemnofgbcjkhilpqasdfrstuvrhfwx";
console.log([...new Set(s.split("").filter(e => e.match(/[;, ]/)))].length==1);
//return [...new Set(s.split("").filter(e => e.match(/[a-z]/)))].length==26;
}

Create a regex to extract a string that contain a noral character and escaped string without DOS

I have a string like this:
///////AB?\a\b\c\d\d\e\\f\a\a\b\cd\ed\fmnopqrstuvwxy\z\a\a\a\a\a\a\a\a\a///imgy
it started with /// and ended with ///imgy (i and/or m and/or g and/or y), and between the beginning and end are the character are normal character like a or escaped character like \a.
Here is my regex:
/^\/{3}((?:\\?[\s\S])+?)\/{3}([imgy]{0,4})(?!\w)/
But the problem is that it is reported as "vulnerable to denial-of-service attacks". The main part that has the problem is
(?:\\?[\s\S])+
How can I create a right one that can figure out both a and \a? Thank you!
Regex Demo
Update:
I just found to use the following regex:
(?:\\[\s\S]+?)|(?:(?<!\\)[\s\S]+?)|(?:(?<=\\\\)[\s\S]+?)
to replace the old problematic part (?:\\?[\s\S])+?, and in this way, it can avoid requires exponential time to match certain inputs, and avoid vulnerable to denial-of-service attacks.
The details:
(?:\\[\s\S]+?) match any \a
(?:(?<!\\)[\s\S]+?) match any a, but not following \.
(?:(?<=\\\\)[\s\S]+?) match any a, but much following \\. This to make sure f is matched that following \\.
So the whole regex will look like this:
^\/{3}((?:\\[\s\S]+?)|(?:(?<!\\)[\s\S]+?)|(?:(?<=\\\\)[\s\S]+?))\/{3}([imgy]{0,4})(?!\w)
You might list the characters that are allowed to a character class, and optionally repeat an escaped character [a-z]
^\/{3,}[A-Za-z?]+(?:\\[a-z\\][A-Za-z?]*)*\/\/\/[imgy]{0,4}$
The pattern matches:
^ Start of string
\/{3,}[A-Za-z?]+ Match 3 or more / and 1 or more times any of the listed allowed chars
(?: Non capture group
\\[a-z\\] Match an escaped char a-z or \\
[A-Za-z?]* Optionally match any of the listed
)* Close an optionally repeat the group
\/\/\/[imgy]{0,4} Match /// and 0-4 times any of i m g or y If there should be at least a single char, you can use {1,4}
$ End of string
Regex demo

Regex to disallow certain characters in specific sequence

I have a regular expression that allows only letters, numbers, spaces or hyphens. However, I'd like to disallow the user to do the following:
hello--world Have more than one hyphen sitting next to each other
--hello Have a hyphen in the beginning. It must have a number or letter first
How do I accomplish this? My current regex looks like this:
let alphanumericTest = new RegExp("^\s*([0-9a-zA-Z- ]*)\s*$");
You can try this regex expression. ^\s*[0-9a-zA-Z](?:(?!--)[0-9a-zA-Z- ])*$
This is a demo.
You could make you match a bit more efficient without using a negative lookahead for matching non consecutive hyphens using repeating groups which can optionally start with an hyphen after the first word.
^[ ]*[0-9a-zA-Z]+(?:-[0-9a-zA-Z]+)*-?(?:[ ]+-?(?:[0-9a-zA-Z]+-?)*)*$
(Used [ ] to match a space for clarity)
Explanation
^ Start of string
[ ]* Match 0+ spaces
[0-9a-zA-Z]+ Match 1+ times any of the listed
(?:-[0-9a-zA-Z]+)* Repeat 0+ times matching a hyphen and 1+ what is listed
-? Match optional hyphen
(?: Non capturing group
[ ]+-?(?:[0-9a-zA-Z]+-?)* Match 1+ spaces, optional hyphen, repeat 0+ times what is listed and optional hyphen
)* Close outer non capturing group and repeat 0+ times
$ End of string
Regex demo
Try:
let alphanumericTest = new RegExp("^(?!-)(?!.*--)[0-9a-zA-Z- ]+(?<!-)$");
This checks that the first character is not a - and that there are no consecutive --s anywhere in the string

How to match any string that contains no consecutively repeating letter

My regular expression should match if there aren't any consecutive letters that are the same.
for example :
"ploplir" should match
"ploppir" should not match
so I use this regular expression:
/([.])\1{1,}/
But It does the exact contrary of what I want. How can I make the match work correctly?
Code
See regex in use here
\b(?!\w*(\w)\1)\w+\b
var r = /\b(?!\w*(\w)\1)\w+\b/g
var s = "ploplir ploppir"
console.log(s.match(r))
Explanation
\b Assert position as a word boundary
(?!\w*(\w)\1\w*) Negative lookahead ensuring what follows doesn't match
\w* Match any number of word characters
(\w) Capture a word character into capture group 1
\1 Match the same text as most recently matched by the 1st capture group
\w+ Match one or more word characters
\b Assert position as a word boundary
Maybe you could use lookarounds to check if there are no consecutive letters in the string:
^(?!.*(.)(?=\1)).*$
Explanation
From the beginning of the string ^
A negative look ahead (?!
Which asserts that following .* a character (.) is not followed by the same character (?=\1) using the group reference \1
Close the negative lookahead
Match zero or more characters .*
The end of the string

Categories

Resources