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

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}$

Related

Regular Expression in JavaScript having exactly 6 digits and at least two different digits [duplicate]

This question already has answers here:
Regular expression limit string size
(3 answers)
Closed 2 years ago.
I want to make sure that I have exactly 6 digits and not all of them are the same (222222 is not acceptable but 122222 or 211111 or 122223 are acceptable).
I am using this:
^(?=\d{6})(\d)\1*(?!\1)\d+$
But this only satisfies minimum 6 digits and not exactly 6 digits.
(source: Regex to match number with different digits and minimum length)
You may use this bit simplified and more efficient regex:
^(\d)(?!\1+$)\d{5}$
RegEx Demo
RegEx Details:
^(\d): Match and capture a digit at the start
(?!\1+$): Negative lookahead to assert that same digit is not repeated till end.
\d{5}$: Add 5 more digits before end

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,'')

Regex for a decimal number [duplicate]

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))

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/

How to grab all numbers from a string in JavaScript? [duplicate]

This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 2 years ago.
Let's say I have an input field and want to parse all of the numbers from the submitted string. For example, it could be:
Hi I'm 12 years old.
How do I parse all of the numbers without having a common pattern to work with?
I tried:
x.match(/\d+/)
but it only grabs the 12 and won't go past the next space, which is problematic if the user inputs more numbers with spaces in-between them.
Add the g flag to return all matches in an array:
var matches = x.match(/\d+/g)
However, this may not catch numbers with seperators, like 1,000 or 0.123
You may want to update your regex to:
x.match(/[0-9 , \.]+/g)
var words = sentence.split(" ");
var numbers = words.filter(function(w) {
return w.match(/\d+/);
})

Categories

Resources