Regex pattern does not tidy up YouTube URLs - javascript

I am trying to replace all parts before youtube.com in a URL with nothing, but my regex expression seems to do nothing.
For example:
https://www.youtube.com/watch?v=k5iJV2cYCt8 -> youtube.com/watch?v=k5iJV2cYCt8
http://www.youtube.com/watch?v=k5iJV2cYCt8 -> youtube.com/watch?v=k5iJV2cYCt8
www.youtube.com/watch?v=k5iJV2cYCt8 -> youtube.com/watch?v=k5iJV2cYCt8
function setYoutubeUrl(youtubeUrl: string) {
return youtubeUrl.replace(
/((\/+|https??:\/\/)?(www\.)?(?=youtube\.com\/.+\/.)|(?<=youtube\.com\/.+\/.)\/*([?#].*)?)/gi,
''
);
}
console.log(setYoutubeUrl("https://www.youtube.com/watch?v=k5iJV2cYCt8"));

Try to use the regex /^(?:https?:\/\/)?(?:www\.)?/i. So your function will be like this:
function setYoutubeUrl(youtubeUrl) {
return youtubeUrl.replace(
/^(?:https?:\/\/)?(?:www\.)?/i,
''
);
}
Regex Explanation:
^ asserts position at start of the string
Non-capturing group (?:https:\/\/)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
http matches the characters http literally (case insensitive)
s matches the character s literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
: matches the character : literally (case insensitive)
\/ matches the character / literally (case insensitive)
\/ matches the character / literally (case insensitive)
Non-capturing group (?:www\.)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
www matches the characters www literally (case insensitive)
\. matches the character . literally (case insensitive)
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

Related

matching two words in a javascript regex

I have a url string that looks like the https://www.bla.com/?test=dfsdf
I want to try and match if www.bla.com and ? is in the url.
The url should only match if its got www.bla.com/? or www.bla.com?
it should not match is its anything else follows like www.bla.com/test/?
First try was.
regex = /bla.com\?/g
www\.bla\.com\/?\?
https://regex101.com/r/tcUDdq/1
www matches the characters www literally (case sensitive)
. matches the character . literally (case sensitive)
bla matches the characters bla literally (case sensitive)
. matches the character . literally (case sensitive)
com matches the characters com literally (case sensitive)
? /? matches the character / literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
? \? matches the character ? literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

How to use regular expressions in VS Code

I have a question about regular expressions. I'm using VS Code and trying to search and replace € (price).
I have a JSON file and in that file a have price like this:
"price": 120.000 € a trying to remove € so everything should look like this:
"price": 120.000
Please help, thank you all.
("price"\s?:\s?"(?:\d+\.?)+)(\s?€)" to be replaced by $1"
$1 is the first captured group. In that RegEx, this is : ("price"\s?:\s?"(?:\d+\.?)+)
1st Capturing Group ("price"\s?:\s?"(?:\d+\.?)+) :
-> "price" matches the characters "price" literally (case sensitive)
-> \s matches any whitespace character (equal to [\r\n\t\f\v ])
--> ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
-> : matches the character : literally (case sensitive)
-> \s matches any whitespace character (equal to [\r\n\t\f\v ])
--> ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
-> " matches the character " literally (case sensitive)
Non-capturing group (?:\d+\.?)+
-> + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
--> \d+ matches a digit (equal to [0-9])
--> \.? matches the character . literally (case sensitive)
2nd Capturing Group (\s?€)
-> \s matches any whitespace character (equal to [\r\n\t\f\v ])
--> ? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)
-> € matches the character € literally (case sensitive)
-> " matches the character " literally (case sensitive)
This will be replaced by $1" which is the first captured group followed by "
Test it yourself

email regular expression in javascript based on mentioned points

Could any one please provide the regular expression for below conditions
User-part does not begin or end with a “.” Character.
Domain-part does not contain the following invalid characters: "#\"%|<>?'`/
,*&;:£$^!~ ú(){}+"
Valid email has ‘#’ and ‘.’ Character
Valid email does not contains two consecutive dots
Currently I am using the below reg ex
/^(?!^[.])[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]{2,63})*$/;
The mentioned regular expression is not working all points.
To respect all these conditions you need to treat them in order and take a logic where you write your regex in parts, I tried to make a suitable solution for your case and this is the Regex I came up with:
(?!^[.])[a-zA-Z_\-0-9]+[a-zA-Z_\-0-9.]+(?!\.\.)[^\.]+#(?![#\"%|<>?'`\/ ,*&;:£$^!~ ú(){}+])(?![\.]{2})[a-zA-Z_\-0-9.]+\.[a-zA-Z]{2,3}
Demo:
You can test this Regex here and see it in a working live Demo too.
Explanation:
Negative Lookahead (?!^[.])
Assert that the Regex below does not match ^ asserts position at start
of the string Match a single character present in the list below [.] .
matches the character . literally (case sensitive)
Match a single character present in the list below [a-zA-Z_\-0-9]+
I added this part to avoid matching the . in the beginning of the
regex
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z a single character in
the range between a and z (case sensitive), A-Z
a single character in the range between A and Z
_ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the
range between 0 and 9
Match a single character present in the list below [a-zA-Z_\-0-9.]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z a single character in
the range between a and z (case sensitive), A-Z
a single character in the range between A and Z
_ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the
range between 0 and 9, . matches the character . literally (case sensitive)
Negative Lookahead (?!\.\.)
Assert that the Regex below does not match \. matches the character .
literally (case sensitive) \. matches the character . literally
Match a single character not present in the list below [^\.]+
+ Quantifier — Matches between one and unlimited times, as many
times as possible, giving back as needed (greedy) \. matches the
character . literally
# matches the character # literally
Negative Lookahead (?![#\"%|<>?'\/ ,*&;:£$^!~ ú(){}+])
Assert that the Regex below does not match Match a single character
present in the list below [#\"%|<>?'/ ,*&;:£$^!~ ú(){}+]`
Negative Lookahead (?![\.]{2})
Assert that the Regex below does not match Match a single character
present in the list below [\.]{2}, {2} Quantifier — Matches exactly 2
times \. matches the character . literally
Match a single character present in the list below [a-zA-Z_\-0-9.]+
+ Quantifier — Matches between one and unlimited times, as many
times as possible, giving back as needed a-z a single
character in the range between a and z (case
sensitive), A-Z a single character in the range between A
and Z (case sensitive)
_ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the
range between 0 and 9, . matches
the character . literally (case sensitive) \. matches the character .
literally
Match a single character present in the list below [a-zA-Z]{2,3}
{2,3} Quantifier — Matches between 2 and 3 times, as many times as
possible, giving back as needed a-z a single character in the
range between a and z (case sensitive), A-Z a
single character in the range between A and Z
var component = {
input : $('input[name="email"]'),
mensage : {
fields : $('.msg'),
success : $('.success'),
error : $('.error')
}
},
regex = /^[a-z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)?#[a-z][a-zA-Z-0-9]*\.[a-z]+(\.[a-z]+)?$/;
component.input.blur(function () {
component.mensage.fields.hide();
regex.test(component.input.val()) ? component.mensage.success.show() : component.mensage.error.show();
});
.msg {
display: none;
}
.error {
color: red;
}
.success {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="email" id="email">Hey!</label>
<input id="email" name="email" type="email" class="required" />
<span class="msg error">You shall not pass!</span>
<span class="msg success">You can pass!</span>
Hope this helps!

Regex multiline javascript M3U list

I have this:
#EXTM3U
#EXTINF:-1,ABC HD
http://AAAAAAAAAAAAAAA/BBBBBBBBBBBBBB/C.ts
#EXTINF:-1,DEF HD
http://FFFFFFFFFFFFFF/DDDDDDDDDDDDDDDDD/C.ts
#EXTINF:-1,GHI HD
http://SSSSSSSSSSSSSS/GGGGGGGGGGGGGG/C.ts
I need a regual expression in javascript with two capturing groups:
(1) ABC HD
(2) http://AAAAAAAAAAAAAAA/BBBBBBBBBBBBBB/C.ts
(1) DEF HD
(2) http://FFFFFFFFFFFFFF/DDDDDDDDDDDDDDDDD/C.ts
..... and so on.
Regex
(http:\/\/(?:[a-zA-Z\/]+)\.ts)
Description
\# matches the character # literally
EXTINF: matches the characters EXTINF: literally (case sensitive)
[^,]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
, the literal character ,
, matches the character , literally
1st Capturing group ([^\n]+)
[^\n]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\n matches a line-feed (newline) character (ASCII 10)
\n matches a line-feed (newline) character (ASCII 10)
2nd Capturing group (http:\/\/(?:[a-zA-Z\/]+)\.ts)
http: matches the characters http: literally (case sensitive)
\/ matches the character / literally
\/ matches the character / literally
(?:[a-zA-Z\/]+) Non-capturing group
[a-zA-Z\/]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
\/ matches the character / literally
\. matches the character . literally
ts matches the characters ts literally (case sensitive)
Output
MATCH 1
1. [19-25] `ABC HD`
2. [26-68] `http://AAAAAAAAAAAAAAA/BBBBBBBBBBBBBB/C.ts`
MATCH 2
1. [80-86] `DEF HD`
2. [87-131] `http://FFFFFFFFFFFFFF/DDDDDDDDDDDDDDDDD/C.ts`
MATCH 3
1. [143-149] `GHI HD`
2. [150-191] `http://SSSSSSSSSSSSSS/GGGGGGGGGGGGGG/C.ts`

Convert Java Regex to javascript regex

([a-zA-Z0-9_\\-])([a-zA-Z0-9_\\.+~!#/$%^&*_=\\'?\\-]*)#[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z0-9]{2,})$
which is working fine for Java but it is not working for JavaScript might be backward slash have a some problem, please tell me how I can convert above java regex into Java Script.
Just reduce the double backslashes to singles. Also, you don't need to escape hyphen if it's the last character in a character class. Also also, you don't need to escape wildcard characters in a character class
Something like this
/([a-zA-Z0-9_-])([a-zA-Z0-9_.+~!#/$%^&*_='?-]*)#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/
It depends on how you are using this? In what code?
you probably just need one \ everywhere you have two \\.
var re = new RegExp("ab+c");
or
var re = /ab+c/;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Regex Demo
var myregexp = /([a-zA-Z0-9_\-])([a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]*)#[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/;
Debuggex Demo
Description
1st Capturing group ([a-zA-Z0-9_\-])
[a-zA-Z0-9_\-] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
\- matches the character - literally
2nd Capturing group ([a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]*)
[a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]* match a single character present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
0-9 a single character in the range between 0 and 9
_ the literal character _
\. matches the character . literally
+~!# a single character in the list +~!# literally
\/ matches the character / literally
$%^&*_= a single character in the list $%^&*_= literally (case sensitive)
\' matches the character ' literally
? the literal character ?
\- matches the character - literally
# matches the character # literally
[A-Za-z0-9-]+ match a single character present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
0-9 a single character in the range between 0 and 9
- the literal character -
3rd Capturing group (\.[A-Za-z0-9-]+)*
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
\. matches the character . literally
[A-Za-z0-9-]+ match a single character present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
0-9 a single character in the range between 0 and 9
- the literal character -
4th Capturing group (\.[A-Za-z0-9]{2,})
\. matches the character . literally
[A-Za-z0-9]{2,} match a single character present in the list below
Quantifier: Between 2 and unlimited times, as many times as possible, giving back as needed [greedy]
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
0-9 a single character in the range between 0 and 9
$ assert position at end of the string

Categories

Resources