I want to create a regular expression such that it can accept following values:-
100, 100.00, (100),(100.00),$100, $100.00, $(100), $(100.00)
and I am succeed in building the following regular expression:-
/^(\$?(?=\d*(\.\d{1,3})?$))|^(\$?\((?=\d*(\.\d{1,3})?\)$))/
but above regex fails if the value is just $
I want if the value is just $ then it should give not accept it..
But at the same time it should accept blank value also..
Please help me.
Thanks in advance
It seems that your integer part is never optional, so you should make sure the regex makes it mandatory (right now, even the empty string would pass your regex):
/^\$?(?:\d+(?:\.\d{1,3})?$|\(\d+(?:\.\d{1,3})?\)$)/
\d+ means "one or more digits".
I changed my regex as :
^(\$?(?=\d+(\.\d{1,3})?$))|^(\$?\((?=\d+(\.\d{1,3})?\)$))|^((?=\d*(\.\d{1,3})?$))
and its working for me..
Related
I am currently developing a web-application where I work with java, javascript, html, jquery, etc. and at some point I need to check that whether an input matches a known pattern and only proceed if it is true.
The pattern should be [at least one but max 3 numbers between 0-9]/[exactly 4 numbers between 0-9], so the only acceptable variations should be like
1/2014 or 23/2015 or 123/2016.
and nothing else, and I CANNOT accept something like 1234/3012 or anything else, and this is my problem right here, it accepts everything in which it can find the above pattern, so like from 12345/6789 it accepts and saves 345/6789.
I am a total newbie with regex, so I checked out http://regexr.com and this is the code I have in my javascript:
$.validator.addMethod("hatarozat", function(value, element) {
return (this.optional(element) || /[0-9]{1,3}(?:\/)[0-9]{4}/i.test(value));
}, "Hibás határozat szám!");
So this is my regex: /[0-9]{1,3}(?:\/)[0-9]{4}/i
which I built up using the above website. What could be the problem, or how can I achived what I described? I tried /^[0-9]{1,3}(?:\/)[0-9]{4}$/ibut this doesn't seem to work, please anyone help me, I have everything else done and am getting pretty stressed over something looking so simple yet I cannot solve it. Thank you!
Your last regex with the anchors (^ and $) is a correct regex. What prevents your code from working is this.optional(element) ||. Since this is a static thing, and is probably true, so it does not show any error (as || is an OR condition, if the first is true, the whole returns true, the regex is not checked at all).
So, use
return /^[0-9]{1,3}\/[0-9]{4}$/.test(value);
Note you do not need the (?:...) with \/ as the grouping does not do anything important here and is just redundant. The anchors are important, since you want the whole string to match the pattern (and ^ anchors the regex at the start of the string and $ does that at the end of the string.)
You need use the the following special characters in your regex expression:
^ and $
or \b
so 2 regexp will be correct:
/\b[0-9]{1,3}(?:\/)[0-9]{4}\b/i;
or
/^[0-9]{1,3}(?:\/)[0-9]{4}$/i
I need a regex expression which will allow following types of input in javascript :-
1,-,2
1,2,-
-,1,2
-
1,2
1
I need it for validation in my website which will allow only above set of values, Please not there could be only one hypen(-) in any case.
1 or 2 in above case can be any number.
Think this is what you need:
^([\d-]+(,[\d-]+)*)?$
Tested via https://regex101.com/
I found this site:
https://mathiasbynens.be/demo/url-regex
and wanted to use for my url validation the regex from the #diegoperini, because according to the table provided on the top of the site, it is the best regex.
When I try to use it, I get a range value error.
P.S. I am using the following Regex expression:
_^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$_iuS
and the following online validator:
http://regexr.com/
It does show the error place in the regex, but I don't know how to manage it. I tried to swap the both ranges, but it doesn't do the trick.
I would appreciate some help.
P. P. S.
I use the regex in the AngularJS directive to validate url input.
Buried within your character classes, you have this range:
\x{00a1}-\x{ffff}
But it should be:
\u00a1-\uffff
Your expression \x{00a1}-\x{ffff} is not the correct syntax for a hex encoding or a character and as-is means any of "x{}0a1f" plus the range "}-x", but "x" is less than "}" so an error is raised to that effect.
This should work
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$
I want to know how can I get a regular expression for matching number, ., and - only.
I am using this:
/^[0-9\.'S]+$/
by this it working fine but not working for symbol "-".
You simply haven't used the literal dash - (or minus) in the regex. Try:
/^[0-9\.-]+$/
But if you want a proper number, you might want to use a more proper regex:
/^-?[0-9]+(?:\.[0-9]+)?$/
The first regex can accept things such as 3987----.... while the second will not accept it, but will accept things like -87.983274.
That's because - is not part of your character class. You are only using - in the class range (which only includes digits). Also, I don't know what the S and the ' are doing there:
/^[0-9.-]+$/
Also, I can promise you that after taking the time to read through this tutorial regular expressions will seem a lot less confusing to you.
Try the below regex.
/^-?[0-9\.]+$/
I am hoping that this will have a pretty quick and simple answer. I am using regular-expressions.info to help me get the right regular expression to turn URL-encoded, ISO-8859-1 pound sign ("%A3"), into a URL-encoded UTF-8 pound sign ("%C2%A3").
In other words I just want to swap %A3 with %C2%A3, when the %A3 is not already prefixed with %C2.
So I would have thought the following would work:
Regular Expression: (?!(\%C2))\%A3
Replace With: %C2%A3
But it doesn't and I can't figure out why!
I assume my syntax is just slightly wrong, but I can't figure it out! Any ideas?
FYI - I know that the following will work (and have used this as a workaround in the meantime), but really want to understand why the former doesn't work.
Regular Expression: ([^\%C2])\%A3
Replace With: $1%C2%A3
TIA!
Why not just replace ((%C2)?%A3) with %C2%A3, making the prefix an optional part of the match? It means that you're "replacing" text with itself even when it's already right, but I don't foresee a performance issue.
Unfortunately, the (?!) syntax is negative lookahead. To the best of my knowledge, JavaScript does not support negative lookbehind.
What you could do is go forward with the replacement anyway, and end up with %C2%C2%A3 strings, but these could easily be converted in a second pass to the desired %C2%A3.
You could replace
(^.?.?|(?!%C2)...)%A3
with
$1%C2%A3
I would suggest you use the functional form of Javascript String.replace (see the section "Specifying a function as a parameter"). This lets you put arbitrary logic, including state if necessary, into a regexp-matching session. For your case, I'd use a simpler regexp that matches a superset of what you want, then in the function call you can test whether it meets your exact criteria, and if it doesn't then just return the matched string as is.
The only problem with this approach is that if you have overlapping potential matches, you have the possibility of missing the second match, since there's no way to return a value to tell the replace() method that it isn't really a match after all.