Regular Expressions Vue [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 21 hours ago.
The community is reviewing whether to reopen this question as of 10 hours ago.
Improve this question
I'm trying to validate an input with regEx in Vue, which I don't have any idea how to make one and couldn't find online how to match what I want to do.
The thing is I'm trying to validate a price that should be a float with 2 decimal numbers, and it can be 1 number before the . or 9 digits. For example:
0.50
1.00
99999.99
999999999.00
I tried this:
v => (/\d{1,3}(?:[.,]\d{3})*(?:[.,]\d{2})/.test(v))
But doesn't work.
Sorry if my english is not very good. I appreciate the help!

To match 1-9 digits before the dot, and 2 decimal numbers:
^\d{1,9}\.\d{1,2}$
See a regex101 demo.

What do you want? Check the value for matching a number from 0 to 999999999 in the integer part and no more than 2 numbers after "."?
A template assuming that the entire string being checked from the beginning (^) to the end ($) consists of
mandatory initial part, which is either 0 or contains from 1 to 9 digits, and does not start with "0" ;
optional ending of "." and two digits:
^([1-9]\d{0,8}|0)(.\d{1,2})?$

Related

javascript - mail exchange server string validation using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
how can validate a mx server (similar to a domain) in the form of mx*.m**p.com by Regex? The first star can be any number without its length pre-defined 1, 11, 111, 1111, without leading 0s. The 2nd and 3rd stars are single letters in range of 0-9 and a-Z.
Examples:
mx1.m0bp.com
mx321.maBp.com
^mx[1-9][0-9]*\.m[0-9a-zA-Z]{2}p\.com$
^ indicates the start of the string
mx are the expected characters
[1-9] The number must not have a leading zero, so it must start with 1-9
[0-9]* Followed by zero or more other digits
\. The dot must be escaped as it has a special meaning
[0-9a-zA-Z]{2} Exactly two characters with the given range
p\.com again the next expected characters with another escaped dot
$ indicates the end of the string
Including the ^ and $ means you won't get a match from foomx1.m0bp.com or mx1.m0bp.comfoo
You can use the below regex to test the domain:
mx[0-9]+\.m[0-9a-zA-Z]{2}p\.com
console.log(/mx[0-9]+\.m[0-9a-zA-Z]{2}p\.com/gi.test("mx1.m0bp.com"))
console.log(/mx[0-9]+\.m[0-9a-zA-Z]{2}p\.com/gi.test("mx321.maBp.com"))

Regular expression to extract Max and Min values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
67 to 90 or 67 - 90. I need to check for the "-" and the "to" so i can split the values into min and maximum
Showing some work is encouraged on Stack Overflow -- but this is quick and easy; I think this should do it for you:
(\d{1,3}) ?(\-|to) ?(\d{1,3})
(\d{1,3}) - an integer of 1 to 3 digits (change the second number if you want larger or smaller values.
? - an optional space (remove the ? if the space isn't optional)
(\-|to) - a hyphen or the word "to"
? - an optional space (remove the ? if the space isn't optional)
(\d{1,3}) - an integer of 1 to 3 digits (change the second number if you want larger or smaller values.
Example: https://regex101.com/r/yN4sP8/1

Match 11 or 13 digits using regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am writing a code where I am receiving a number of exactly 11 or 13 digits in it. But, the problem is that it may contain some hyphens at random places.
Can anyone suggest a regular expression for this?
Sample inputs (assuming only 5 digits):
1. 12345
2. 1-234-5
3. 12-34-5
4. 123-45
5. 1-2-34-5
Try this code snippet. It may help you.
var str="123-45";
str.replace( /\D+/g, '');
Here,
\D - Find a non-digit character.
so, Code will replace non-digit with ''.
It would be significantly easier, and infinitely more readable to remove all dashes, and then count the remaining characters.
var str = "1-234-5";
var res = str.replace(/-/g, '').length;
if(res === 11 || res === 13) {
//do whatever
}
Have a try with:
^(?:-?\d){11}(?:-?\d-?\d)?$
or, if - can't be in first place:
^(?:\d-?){11}(?:\d-?\d)?$

How do I tell what index a letter is in a string? (In Python, JS, Ruby, PHP etc...) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I know that:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
print alphabet[0]
# prints a
print alphabet[25]
#prints z
and so on, but how do I find out the opposite, ie. :
alphabet = 'abcdefghijklmnopqrstuvwxyz'
's' = alphabet[?]
""" The question mark represents that I want to know
what index the letter is in the string."""
In python you could use the find method:
>>> alphabet = 'abcdefghijklmnopqrstuvwxyz'
>>> alphabet.find('a')
0
>>> alphabet.find('b')
1
>>> alphabet.find('c')
>>> alphabet.find('z')
25
Edit to add: Like Warren pointed out you can also use index, the difference being that find will return -1 for the position if not found, and index raises a ValueError when not found.
In javascript, use indexOf:
> "abc".indexOf("b")
1
In javascript, you would use alphabet.indexOf('a').
In Python, to get the position of a certain character inside a string, it would be:
alphabet.index('s')
If you just want to get alphabet indices, this works (Python):
import string
for c in string.ascii_lowercase:
print(ord(c)-97)
This will print the numbers 0-25. The idea here is to make use of unicode points of the char using ord. lowercase chars starts at 97. Another example:
>>> ord('k')-97
10

Phone Number Validation with Regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a web form and want to accept the following phone numbers in the following format:
1234567890
123-456-7890
123.456.7890
123-4567890
The first number cannot be a 0 or a 1.
How can I do this with regex/javascript? I found a few regex formulas online but none are specific to my needs
null !== thenumber.match(/^[2-9][0-9]{2}[.-]?[0-9]{3}[.-]?[0-9]{4}$/);
(Edited to give slightly better answer with boolean result)
Consider the following Regex...
[\d-[01]]\d{2}[-\.]?\d{3}[-\.]?\d{4}
Note: You examples start with a 1 which will not satisfy the above regex.
The user-provided format should be irrelevant. It makes more sense to store phone numbers as, well, numbers, i.e. digits only, and add uniform formatting when displaying them back. Otherwise you end up wit a mess in your database and you're going to have a hard time searching for numbers (if you wanted to find if given number is already in your DB then how'd you know the format it was typed in?), and you will have inconsistent formatting of numbers when displaying them.
So you'd store any of your example numbers as 1234567890, no matter what the user has typed into the form. Which means you can validate your input by stripping any non-digits, then checking the length and other conditions, like this:
function validPhone( num ){
var digits = num.replace(/\D/g,'');
// assuming 10 digits is a rule you want to enforce and the first digit shouldn't be 0 or 1
return (parseInt(digits[0],10) > 1 && digits.length == 10);
}
You could also use return parseInt(digits, 10) >= 2000000000 to validate that the number doesn't start with 0 nor 1 and has at least 10 digits.

Categories