I am trying to match a complex if else regex pattern using jquery.validation
//My regex
(?=^abc1)(^abc1[-]dba\d*.csv)|(?=^def2)(^def2[-]dba\d*.xls|txt)|(?=^ghi3)(^ghi3[-]dba\d*.xls)|(?=^xyz4)(^xyz4[-]dba\d*.csv)
// Example
abc1-dba.csv
Tried over here : http://www.regexr.com/ it matches
But in the code it is not matching anything.
//I call the validate function inside which :-
rules : {
param : {
required : true,
pattern: "(?=^abc1)(^abc1[-]dba\d*.csv)|(?=^def2)(^def2[-]dba\d*.xls|txt)|(?=^ghi3)(^ghi3[-]dba\d*.xls)|(?=^xyz4)(^xyz4[-]dba\d*.csv)"
}
}
doesn't work.
There are some errors in your regex, but the reason it's failing on abc1-dba10.csv is because you're writing the regex as a string literal and you forgot to escape the backslashes. Your \d* should be \\d*.
As for the errors, the most significant one is where you test for .xls or .txt extensions. You need to isolate that part in its own group, like this: \.(?:xls|txt). You also forgot to escape the period, not just in that spot but everywhere. (You would probably never get bitten by that one, but it is a bug.)
Aside from that, you've got way more start anchors (^) than you need, but you left out the end anchor ($) entirely. And those lookaheads aren't doing anything useful. If the string matches the regex ^abc1[-]dba\d*.csv, then of course a lookahead like (?=^abc) at the beginning of the string will succeed. Here's how I would write it:
"^(?:abc1-dba\\d*\\.csv|def2-dba\\d*\\.(?:xls|txt)|ghi3-dba\\d*\\.xls|xyz4-dba\\d*\\.csv)$
If you can use a regex literal instead, use this:
/^(?:abc1-dba\d*\.csv|def2-dba\d*\.(?:xls|txt)|ghi3-dba\d*\.xls|xyz4-dba\d*\.csv)$/
Related
I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?
im trying to regex this, but it doesnt work:
this is my string:
asdasd2-bgbegebr23-yiyity23-iopip123
So im trying to get: all values between '-', but it doesnt work: im using actually this:
/(-)(.*)(-)/gi
as regex, but doesnt work, thanks everyone :S
That's because the dot includes de dash. You should remove the dash. Try this:
/([^-]+)/gi
I don't understand very well the purpose of your regex. Supposing that you want to desgin a regular expression that iterated over the example string gets succesively asdasd2, bgbegebr23, etc, the regular expression will be something like this:
\-?([^\-]*)\-?
Why??
- : You have to use "-" instead of "-" because the hyphen is a special char for regular expressions, so you have to escape it
? : The hyphen is optional: in the first case (asdasd2), it is not present. So, enforcing it will omit this first case.
() : Grouping to catch all the asdsas2 and that alphanumeric stuff..
[^-] : Everything except hyphen. The approximation of #vks (.*?) I think will work also
- : Again, a hyphen, but escaped. And we cannot forget the "?" at the end because the last case of the example string, which doesn't end in hyphen.
And don't forget that if you are working in javascript you might need to scape the backslash (), resulting in an expression like this:
\\-?[^\\-]*)\\-?
While trying to submit a form a javascript regex validation always proves to be false for a string.
Regex:- ^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
I have tried following strings against it
abc.jpg,
abc:.jpg,
a:.jpg,
a:asdas.jpg,
What string could possible match this regex ?
This regex won't match against anything because of that $? in the middle of the string.
Apparently using the optional modifier ? on the end string symbol $ is not correct (if you paste it on https://regex101.com/ it will give you an error indeed). If the javascript parser ignores the error and keeps the regex as it is this still means you are going to match an end string in the middle of a string which is supposed to continue.
Unescaped it was supposed to match a \$ (dollar symbol) but as it is written it won't work.
If you want your string to be accepted at any cost you can probably use Firebug or a similar developer tool and edit the string inside the javascript code (this, assuming there's no server side check too and assuming it's not wrong aswell). If you ignore the $? then a matching string will be \\\\w\\\\ww.jpg (but since the . is unescaped even \\\\w\\\\ww%jpg is a match)
Of course, I wrote this answer assuming the escaping is indeed the one you showed in the question. If you need to find a matching pattern for the correctly escaped one ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(\.jpeg|\.JPEG|\.jpg|\.JPG)$ then you can use this tool to find one http://fent.github.io/randexp.js/ (though it will find weird matches). A matching pattern is c:\zz.jpg
If you are just looking for a regular expression to match what you got there, go ahead and test this out:
(\w+:?\w*\.[jpe?gJPE?G]+,)
That should match exactly what you are looking for. Remove the optional comma at the end if you feel like it, of course.
If you remove escape level, the actual regex is
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
After ^start the first pipe (([a-zA-Z]:)|(\\{2}\w+)\$?) which matches an alpha followed by a colon or two backslashes followed by one or more word characters, followed by an optional literal $. There is some needless parenthesis used inside.
The second part (\\(\w[\w].*))+ matches a backslash, followed by two word characters \w[\w] which looks weird because it's equivalent to \w\w (don't need a character class for second \w). Followed by any amount of any character. This whole thing one or more times.
In the last part (.jpeg|.JPEG|.jpg|.JPG) one probably forgot to escape the dot for matching a literal. \. should be used. This part can be reduced to \.(JPE?G|jpe?g).
It would match something like
A:\12anything.JPEG
\\1$\anything.jpg
Play with it at regex101. A better readable could be
^([a-zA-Z]:|\\{2}\w+\$?)(\\\w{2}.*)+\.(jpe?g|JPE?G)$
Also read the explanation on regex101 to understand any pattern, it's helpful!
I am trying to build a RegEx in Javascript that does not match if a certain string appear. So I want to match this curly bracket { but only if in front of it is not the string else.
I am trying to do this ^ *[^else]* *{.*$ but in fact this doe not match if any character in elsestring appear, for example this does not match also this:
erai {
I want to match all the cases when { appear despite of this case else {.
Please could you help me. Here is my DEMO
You can use a negative lookahead. This is supported by JavaScript:
(?!\s*else).+ *({).*$|
DEMO
JavaScript RegEx doesn't support ifs but we can use a trick for it to work:
(?!RegExp)
That's the first part, if RegExp (which is a regex) doesn't appear, then we do the code after that:
.+ *({).*$
That's the RegEx we run. Broken does, it is:
.+ Match anything
* Until 0 - unlimited spaces
({) Capture the {
.*$ Match anything till the end
Now this won't work unless we add a | at the end, or an OR. This will trick it into working like an if statement
Debuggex Demo
What you are looking for is called negative lookbehind.
I am using the following regex to validate an email:
^[a-zA-Z0-9!$'*+/\-_#%?^`&=~}{|]+(\.[a-zA-Z0-9!$'*+/\-_#%?^`&=~}{|]+)*#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-['&_\]]]+)(\.[\w-['&_\]]]+)*))(\]?)$
This works fine in C# but in JavaScript, its not working.... and yes I replaced every backslash with a double backslash as the following:
^[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+(\\.[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+)*#((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-['&_\\]]]+)(\\.[\\w-['&_\\]]]+)*))(\\]?)$
I am using XRegExp. Am I missing something here? Is there such thing as a converter to convert normal regex to JavaScript perhaps :) ?
Here is my function:
function CheckEmailAddress(email) {
var reg = new XRegExp("^[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+(\\.[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+)*#((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-['&_\\]]]+)(\\.[\\w-['&_\\]]]+)*))(\\]?)$")
if (reg.test(email) == false) {
return false;
}
return true;
}
It is returning false for a simple "abc#123.com" email address.
Thanks in advance!
Kevin
The problem is that your regular expression contains character class subtractions. JavaScript's RegExp does not support them, nor does XRegExp. (I initially misremembered and commented that it does, but it does not.)
However, character class subtractions can be replaced with negative lookaheads so this:
[\w-['&_\\]]]
can become this:
(?:(?!['&_\\]])\\w)
Both mean "any word character but not one in the set '&_]". The expression \w does not match ', & or ] so we can simplify to:
(?:(?!_)\\w)
Or since \w is [A-Za-z0-9_], we can just remove the underscore from the list and further simplify to:
[A-Za-z0-9]
So the final RegExp is this:
new RegExp("^[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+(\\.[a-zA-Z0-9!$'*+/\\-_#%?^`&=~}{|]+)*#((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([A-Za-z0-9]+)(\\.[A-Za-z0-9]+)*))(\\]?)$")
I've done modest testing with this RegExp, but you should do due diligence on checking it.
It is not strictly necessary to go through the negative lookahead step to simplify the regular expression but knowing that character class subtractions can be replaced with negative lookaheads is useful in more general cases where manual simplification would be difficult or brittle.