Hello I am trying to create a regex that recognizes money and numbers being inputted. I have to allow numbers because I am expecting non-formatted numbers to be inputted programmatically and then I will format them myself. For some reason my regex is allowing a one letter character as a possible input.
[\$]?[0-9,]*\.[0-9][0-9]
I understand that my regex accepts the case where multiple commas are added and also needs two digit after the decimal point. I have had an idea of how to fix that already. I have narrowed it down to possibly the *\.
as the problem
EDIT
I found the regex expression that worked [\$]?([0-9,])*[\.][0-9]{2}
but I still don't know how or why it was failing in the first place
I am using the .formatCurrency()
to format the input into a money format. It can be found here but it still allows me to use alpha characters so i have to further masked it using the $(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
where input mask is found here and $(this)
is a reference to a input element of type text. My code would look something like this
<input type="text" id="123" data-Money="true">
//in the script
.find("input").each(function () {
if ($(this).attr("data-Money") == "true") {
$(this).inputmask('Regex', { regex: "[\$]?([0-9,])*[\.][0-9]{2}" });
$(this).on("blur", function () {
$(this).formatCurrency();
});
I hope this helps. I try creating a JSfiddle but Idk how to add external libraries/plugin/extension
Firstly, you can replace '[0-9]' with '\d'. So we can rewrite your first regex a little more cleanly as
Breaking this down:
From this, we can see that the minimum legal string is
\.\d\d
, three characters long. The regex you gave will never validate against any one character string.Looking at your second regex,
This has the exact same minimum matchable string as above -
\.\d\d
.edit: As mentioned, depending on the language you may need to escape forward slashes to ensure they aren't misinterpretted by the language when processing the string.
Also, as an aside, the below regex is probably closer to what you need.
Explanation:
The negative lookahead was provided by an answer from John Kugelman in this question.
This correctly matches (matches enclosed in square brackets):
But not:
The "regular expression" you're using in your example script isn't a RegExp:
Rather, it's a String which contains a pattern which at some point is being converted into a true RegExp by your library using something along the lines of
Within Strings a backslash
\
is used to represent special characters, like\n
to represent a new-line. Adding a backslash to the beginning of a period, i.e.\.
, does nothing as there is no need to "escape" the period.Thus, the RegExp being created from your String isn't seeing the backslash at all.
Instead of providing a String as your regular expression, use JavaScript's literal regular expression delimiters.
So rather than:
use
And I believe your "regular expression" will perform as you expect.
(Note the use of forward slashes
/
to delimit your pattern, which JavaScript will use to provide a true RegExp.)