简体   繁体   English

正则表达式在 javascript 中查找负数

[英]Regex To Find negative number in javascript

I am trying to solve below issue using javascript, let me know if someone came across similar problem and any pointer will be really helpful.我正在尝试使用 javascript 解决以下问题,如果有人遇到类似问题请告诉我,任何指针都会很有帮助。

I am working on Currency format where different country can different representation for negative currency format.我正在研究货币格式,不同的国家/地区可以对负货币格式进行不同的表示。

Requirement: If a number start /end with - consider such value as negative.要求:如果数字以 - 开头/结尾-将此类值视为负数。 Similarly if a number start / end with () consider such value as negative.同样,如果数字以()开头/结尾,则将此类值视为负数。

-123.55 = > Negative True 
123.55 = > Negative False 
(123.55) => Negative True

My code :我的代码

 console.log('1234,56' + ' vs ' + formatNumber('1234,56', '', ',')); console.log('1234.56' + ' vs ' + formatNumber('1234.56', '', '.')); console.log('-1,234.567' + ' VS ' + formatNumber('-1,234.567', ',', '.')); console.log('1,234.567-' + ' VS ' + formatNumber('1,234.567-', ',', '.')); console.log("1'234,567-" + " VS " + formatNumber("1'234,567-", "'", ',')); console.log("1'234.567-" + " VS " + formatNumber("1'234.567-", "'", '.')); console.log("1.234,567-" + " VS " + formatNumber("1.234,567-", ".", ',')); function formatNumber(sourceString, groupseperator, decimalSeperator) { let negativeNumber = false; var negRegexp = new RegExp("[-|)]$", "g"); var match = negRegexp.exec(sourceString); if (match && match.length > 0) { console.log(match[0]); negativeNumber = true; } //Step1:Replace braces space from the string sourceString = sourceString.replace(/[`\s-()'\]\\\/]/gi, ''); //Step2:Replace All Group Seperator with blank sourceString = sourceString.replace(new RegExp(`[${groupseperator}]`, 'g'), ''); //Step 3: Replace decimal seperator with.dot //Decimal Seperator will only replace from the last occurance sourceString = sourceString.replace(new RegExp(`${decimalSeperator}([^${decimalSeperator}]*)$`), '.$1'); if (negativeNumber) { sourceString = '-' + sourceString; } //console.log(sourceString); //console.log(Number(sourceString)); return Number(sourceString); }

If you refer code below scenario is failing.如果您参考下面的代码场景失败。

console.log('-1,234.567' + ' VS '+ formatNumber('-1,234.567',',','.')); console.log('-1,234.567' + ' VS '+ formatNumber('-1,234.567',',','.'));

It is returning it as positive number它以正数返回

negRegexp is wrong. negRegexp是错误的。 It matches - at the end, but not the beginning.它匹配-在最后,但不是开始。

Use separate alternatives in the regexp for each criteria.在正则表达式中为每个条件使用单独的替代项。

 var negRegexp = /^-|-$|^\(.*\)$/; console.log(negRegexp.test('-123.55')); // Negative True console.log(negRegexp.test('123.55')); // Negative False console.log(negRegexp.test('(123.55)')); // Negative True console.log(negRegexp.test("1'234,567-")); // Negative True

The first alternative matches - at the beginning, the second matches - at the end, and the third matches ( at the beginning and ) at the end.第一个替代匹配-在开头,第二个匹配-在结尾,第三个匹配(在开头和)在结尾。

There's no need for the g flag since you're only matching one number.不需要g标志,因为您只匹配一个数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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