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].)
Related
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
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;
}
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
I have got this string {bgRed Please run a task, {red a list has been provided below}, I need to do a string replace to remove the braces and also the first word.
So below I would want to remove {bgRed and {red and then the trailing brace which I can do separate.
I have managed to create this regex, but it is only matching {bgRed and not {red, can someone lend a hand?
/^\{.+?(?=\s)/gm
Note you are using ^ anchor at the start and that makes your pattern only match at the start of a line (mind also the m modifier). .+?(?=\s|$) is too cumbersome, you want to match any 1+ chars up to the first whitespace or end of string, use {\S+ (or {\S* if you plan to match { without any non-whitespace chars after it).
You may use
s = s.replace(/{\S*|}/g, '')
You may trim the outcome to get rid of resulting leading/trailing spaces:
s = s.replace(/{\S*|}/g, '').trim()
See the regex demo and the regex graph:
Details
{\S* - { char followed with 0 or more non-whitespace characters
| - or
} - a } char.
If the goal is go to from
"{bgRed Please run a task, {red a list has been provided below}"
to
"Please run a task, a list has been provided below"
a regex with two capture groups seems simplest:
const original = "{bgRed Please run a task, {red a list has been provided below}";
const rex = /\{\w+ ([^{]+)\{\w+ ([^}]+)}/g;
const result = original.replace(rex, "$1$2");
console.log(result);
\{\w+ ([^{]+)\{\w+ ([^}]+)} is:
\{ - a literal {
\w+ - one or more word characters ("bgRed")
a literal space
([^{]+) one or more characters that aren't {, captured to group 1
\{ - another literal {
\w+ - one or more word characters ("red")
([^}]+) - one or more characters that aren't }, captured to group 2
} - a literal }
The replacement uses $1 and $2 to swap in the capture group contents.
jQuery Regex
/((\b([a-zA-Z]{0,15})\b)([^a-z0-9\$_]))/g
My Attempt So Far: https://regex101.com/r/d3VUpG/1
Example test string:
(options.method==="
|options.method==="
=options.method==="HEAD"
options.method.options.method==="HEAD"
What I'm Trying TO Achieve
Returned as $1 the value of any connected words such as:
options.method - Would = $1
options.method.options.method - Would also = $1
Question
How can I find all words connected with a dot (.) to then wrap in a span like the below example;
.replace(//gi,'<span class="join">$1</span>')
You can use the following expression:
/((?:\w+\.)+\w+)/g
Explanation:
( - Start of capturing group 1
(?: - Start of a non-capturing group
\w+\. - Match [a-zA-Z0-9_] characters one or more times followed by a literal . character
)+ - End of the non-capturing group; match the group one or more times
\w+ - Match [a-zA-Z0-9_] characters one or more times
) - End of capturing group 1
So in other words, the non-capturing group, (?:\w+\.)+, will match a substring like option. one or more times followed by a final \w+ which will match the final word without a literal . character following it. Since there is only one capturing group wrapping everything, you can wrap your span tag around the first group, $1.
Live Example
string.replace(/((?:\w+\.)+\w+)/g, '<span class="join">$1</span>');
As mentioned above, \w includes underscore, numbers and letters ([a-zA-Z0-9_]), so if you only want to match letter characters, then you could swap out \w with [a-z] and use the case-insensitive flag:
/((?:[a-z]+\.)+[a-z]+)/gi