Regular expression to match dollar amounts

2024/2/27 7:44:54

Could anybody provide me the regular expression for the following patterns?

$1234

$31234.3

$1234.56

$123456.78

$.99

My requirement is the digits before decimal should not exceed 6 and after the decimal point it should not exceed 2 digits. Please help me. Thanks in advance..

Answer
^\$(?=.*\d)\d{0,6}(\.\d{1,2})?$

(?=.*\d) makes sure that there is at least one digit in the string. Without that, the regex ^\$\d{0,6}(\.\d{1,2})?$ would match the string $.

Btw, the lookahead need not be this long; even a simple (?=.) would do, as the regex makes sure that the subsequent characters are indeed valid. Thus, it can be simplified to

^\$(?=.)\d{0,6}(\.\d{1,2})?$
http://en.ppmy.cn/q/41449.html

Related Q&A

How can I render/update my table using REACT?

Im not able to do multi cross filtering, when I click on Apply (to apply all my selected options from my dropdowns) or Cancel button (to reset the selected options). For example filter by taste and ava…

Will a simple function declaration form a closure in JavaScript?

Will the following code form a closure?function f() {}Does where this function is defined affect the answer?

Is it possible to use a for loop in select in html? and how?

I am trying to use a for loop in html but i dont even know if this is possible. Is it? and if yes how? I dont want to use php. only html and javascript. this is my goal: i have a file containing .tx…

Next.js dynamic page params for static export

I have page which depends on route params (ex.: slug) like so http://example.com/blog/:slug. This route path is defined properly in my next.config.js file:module.exports = withPlugins(plugins, {exportP…

An unhandled exception occurred: Could not find module @angular-devkit/build-angular

When running the terminal commands ng server or ng serve Im getting this issue:An unhandled exception occurred: Could not find module "@angular-devkit/build-angular"

Javascript sort callback

I need to sort a big list of Javascript items and Im using the sort function like this:var sorted_list = non_sorted.sort(function(a,b){ // Sort stuff here });What Id like to do is to call a function wh…

How to pass data from ASP.NET WebForms to Aurelia Global Scope

I am bootstrapping Aurelia from a Web-forms based legacy application. My authentication related information is maintained in the web-forms application in the Custom Base Page class. How do I pass &…

How can I access style properties on javascript objects which are using external style sheets?

I have an external style sheet with this in it:.box { padding-left:30px; background-color: #BBFF88; border-width: 0; overflow: hidden; width: 400px; height: 150px; }I then have this:<div id="0&…

How do I get VML working in standards mode?

I would like to be able to use vml objects on a page rendering in standards mode rather than quirks mode. Ive found fragments of answers scattered around but cant figure it out. Raphael pulls it off so…

jquery - slideToggle not smooth

I have a problem with the slideToggle function in jQuery. It is not smooth at all. Whenever I click the "See More" button, the content that should slideToggle, just pops out with no effect wh…