简体   繁体   中英

How can I limit the decimal point in a regex?

When a decimal point is not allowed at the beginning or end, it can be in the middle and there must be only one decimal point.

I used regular expressions to create the expression I wanted. Numbers must be entered, but no English characters or other string values ​​can be used. Only one decimal point can be used, but I do not want to allow a decimal point at the beginning. But the last one is allowed to be inserted. One decimal point in the middle of a number with a trailing decimal point is allowed. In addition, even if there is no decimal point in the middle of a number, it is allowed to have a decimal point at the end. like this

(o )13.4. 13.
(x) .

However, when using my regular expression, the decimal point is used more than once, and the decimal point is also used at the beginning and end.

this is my regex

let regex = /[^\d.]/g;

how can i fix this?

Let me summarize your requirements as your message and provided expression is not easy to understand or conflicting with each other.

allowed are values, which…

  • in general contain nothing else than numbers and maybe dots
  • just number(s), eg. 1 or 12
  • just number(s) with trailing dot, eg. 1. or 12.
  • number(s) with decimal(s), eg. 1.2 or 12.3 or 12.34
  • number(s) with decimal(s) and trailing dot, eg. 1.2. or 12.3. or 12.34.

disallowed are values, which…

  • are empty
  • start with a dot
  • contain repeating dots
  • contain more than two dots

If that is correct, then I would go with the following expression.

/^\d+(?:\.\d+)?\.?$/

Have you considered that negative numbers could appear in you data? If that is the case then take the following expression

/^-?\d+(?:\.\d+)?\.?$/

You can remove the ?: parts if you do not mind about the capturing groups and look for better readability.

const str = '123.12';

const regex = new RegExp('^\\d+([.]\\d+)?$');

console.log(regex.test(str));

in my. you might find a way to fix your problem ,but regex sometimes is not the best solution. if possible ,just write a method to limit , 1.only numbers or point are allowed 2.point is only one time in the string, but not begin or the end

it's might not hard for you, and trust me it would be fast.

here are some exp:

 if(str[0]==='.' || str[str.length -1] === '.'){ return false; } let pointCount = 0; for(let i=0; i<str.length; i++){ let uniCode = str.charCodeAt(i); // . if(uniCode === 46){ pointCount ++; } // number and point if(uniCode < 48 && uniCode > 57 && uniCode !== 46){ return false; } } if(pointCount > 1){ return false; }

I just added the answer that includes the negative form all the cases that you include in your question.

 str = '13.47'; regex2 = new RegExp('^\-?\[0-9]+([.])?(\[0-9]?)+$'); console.log(str,regex2.test(str)); str = '-13.4'; console.log(str,regex2.test(str)); str = '-13'; console.log(str,regex2.test(str)); str = '-1d3'; console.log(str,regex2.test(str)); str = '.13'; console.log(str,regex2.test(str)); str = '13.'; console.log(str,regex2.test(str)); str = '13..'; console.log(str,regex2.test(str)); // to accept one decimal console.log('Example for accepting one decimal'); str = '13.4'; regex2 = new RegExp('^\-?\[0-9]+([.])?\[0-9]?$') console.log(str,regex2.test(str)); str = '13.42'; console.log(str,regex2.test(str)); str = '13.'; console.log(str,regex2.test(str)); str = '-13'; console.log(str,regex2.test(str)); str = '.13'; console.log(str,regex2.test(str)); str = '.13..'; console.log(str,regex2.test(str)); str = '13..'; console.log(str,regex2.test(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