Convert Java Regex to javascript regex - javascript

([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

Related

Regex Javascript Starts with AB 0-9 and then ends with A-Z as optional it could be AB00001 or AB00001A (as optional)

^AB+[0-9A-Z?]+$
Javascript Regex Starts with AB 0-9 and then ends with A-Z as optional it could be AB00001 or AB00001A (as optional)
Strings which should work AB00001 , AB000001B , AB12122 , AB00001C with any capital letter as a n optional
The pattern that you tried ^AB+[0-9A-Z?]+$ matches:
Single A char
1+ times B char
1+ times any of the specified ranges 0-9 A-Z or a ? char.
It can match for example ABB???
You could write the pattern matching a single A and single B char, then 1+ digits and optionally match a char A-Z according to the example data.
^AB\d+[A-Z]?$
See a regex demo

JavaScript regex with below rules

Need to create a regex for a string with below criteria
Allowable characters:
uppercase A to Z A-Z
lowercase a to z a-z
hyphen `
apostrophe '
single quote '
space
full stop .
numerals 0 to 9 0-9
Validations:
Must start with an alphabetic character a-zA-Z or apostrophe
Cannot have consecutive non-alpha characters except for a full stop followed by a space.
The regex I have from the previous question in this forum. Business came back and want to allow string starting with apostrophe along with [a-zA-Z]. This break some previous validations.
eg: a1rte is valid
'tyer4 is valid
'4rt is invalid
^(?!.*[0-9'`\.\s-]{2})[a-zA-Z][a-zA-Z0-9-`'.\s]+$
Please advise.
You might use
^(?=[a-zA-Z0-9`'. -]+$)(?!.*[0-9'` -]{2})[a-zA-Z'][^\r\n.]*(?:\.[ a-z][^\r\n.]*)*$
Explanation
^ Start of string
(?=[a-zA-Z0-9`'. -]+$) Assert only allowed characters
(?!.*[0-9'` -]{2}) Assert not 2 consecutive listed characters
[a-zA-Z'] Match either a char a-zA-Z or apostrophe
[^\r\n.]* Optionally match any char except a newline or a dot
(?:\.[ a-z][^\r\n.]*)* Optionally repeat matching a dot only followed by a space or char a-z
$ End of string
Regex demo

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`

Categories

Resources