简体   繁体   中英

JavaScript regex for numbers with brackets and commas

I gave the input (123,456.12) and my output should be -123456.12 . But its coming as just -123 . I used the expression:

enteredValue = window.event.srcElement.value.replace(/^[-(]\D*([\d.]+).*$/gm, '-$1')

Please help.

You didn't include comma:

.replace(/^[-(]\D*([\d,.]+).*$/gm, '-$1')

http://jsfiddle.net/4bKJp/

Try

 var str = "(123,456.12)".replace( /[(),]/g, "" );
 var num = -Number( str );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM