Regex for a decimal number [duplicate] - javascript

This question already has answers here:
Displaying a number in Indian format using Javascript
(15 answers)
Closed 9 years ago.
I used the following regex
var x=32423332.343;
var res= x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
which gives an output of 32,423,332.343
How do I modify this regex (shortest way) to get the following output
3,24,23,332.343

Well, if you want that, you can modify your regex a bit:
\B(?=(?:\d{2})*\d{3}(?!\d))
regex101 demo
(?:\d{2})* will match even number of digits before the final \d{3}.
For PCRE engine, one that can handle integers and floating, with g enabled.
\G\d{1,2}\K\B(?=(?:\d{2})*\d{3}(?!\d))

Related

How can I extract number from text with regular expression [duplicate]

This question already has answers here:
Get price value from a string
(7 answers)
Closed 3 years ago.
I am trying to extract number from title. I have title like this:
**
Apartment London, 230.400€, 450,00m2
**
I want to extract only this price number (230.400€), can anybody help me with this?
So far my code look like this
\d+\.?\d+(.)
You can include the amount symbol as part of your RegEx:
/\d+\.?\d+(.)€/
Or you can try using Positive lookahead:
\d+\.?\d+(.)(?=€)

Replace dot in a number [duplicate]

This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')

How can I use regular expression for multiple values [duplicate]

This question already has answers here:
Comma Separated Numbers Regex
(6 answers)
Closed 6 years ago.
I am trying to generate a regex which would match the following sequence-
+911111111111,+912222222222,+913333333333,+914444444444
It should not allow any other character other than + and numbers
I have tried this->
/^(\+91)\d{10}$/
But it works only for one phone number not for multiple phone numbers
If
^\+\d{11}$
(a + followed by 11 digits) is not sufficient you'll need to be more specific about what you want to allow and not-allow.
Update following comment: the first two digits are "91" so those can be specified, and then ten further digits:
^\+91\d{10}$

Number format with regex javascript [duplicate]

This question already has answers here:
Javascript Thousand Separator / string format [duplicate]
(15 answers)
Closed 8 years ago.
I have a number is 1205000000, I want display at 1.205.000.000
number.toString().replace(/(\d{3})/g, "$1.").toString()
but result is 120.500.000.0
I don't want reverse a number.
For the sake of correcting your regular expression (obviously for integer values only):
number.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1.');
» Detailed Regex Explanation
One way would be to reverse the string before your manipulation and ther reverse it again. Like so:
var number = 1205000000;
function reverse(s) {
return s.split("").reverse().join("");
}
var str = reverse(reverse(number.toString()).replace(/(\d{3})/g, "$1."));
alert(str);
See this working fiddle.
EDIT:
See the comments. Its a bit dirty but for that specific number it will work. The link posted by #Artyom Neustroev as a comment under you question seems a whole lot better than this here.

Regular expression for numbers with decimal [duplicate]

This question already has answers here:
Regular Expression to accept only positive numbers and decimals
(6 answers)
Closed 9 years ago.
In my phonegap app I need to validate the textbox with regular expression which should contain only numbers and two decimal places.
Here is sample I have tried:
rexp: /^-?\d+\.?\d*$/
This code allows only numbers but after the dot(.) its accepting many numbers. But I should allow only two. Please guide me to solve this issue.
You can make the number of matches explicit using the "{x}" modifier:
rexp: /^-?\d+\.?(\d{2})?$/
I suspect that you want to only have the two digits when there is a dot specified. Then you would modify you regexp as follows:
rexp: /^-?\d+(\.\d{2})?$/
I have created a JSFiddle to show the workings of the regexes and the difference between them: http://jsfiddle.net/q8NAz/

Categories

Resources