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)?$
Related
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})?$
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have built a function that formats a number using commas, similar to what the toLocaleString method does. To achieve that, I used a regular expression and recursion. However, I have a feeling that this could've been done better.
I did some research but was not able to find the answer I'm looking for. So, my question is...Is there a better way to do this?
function transform(value) {
const pureNumber = parseInt(value);
const numberParts = [];
function format(val) {
let formatted = val.toString().split(/(\d{3})$/).filter(i => !!i).join(",");
const splitted = formatted.split(",");
if(splitted.length > 1){
numberParts.unshift(splitted[1]);
return format(splitted[0]);
}
numberParts.unshift(splitted[0]);
return numberParts.join(",");
}
return format(pureNumber.toString());
}
const data = "1234567890";
const result = transform(data);
console.log(result);
What I need you to note is that I used a regular expression to split the string, however, I was wondering if there is a way to only use regular expressions to avoid the recursion? I.e., Is there a way to use the regular expression starting at the end of the string and repeating towards left?
This can be accomplished much simpler using a single Regex expression:
function transform(value) {
return String(value).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
// Works with string
console.log(transform("0123456789"));
// And numbers
console.log(transform(1234567890));
This regex will look in the string for any point that has 3 digits in a row after it and will make sure that point only has exactly multiples of 3 digits.
This was discovered in the first part of a post:
How to print a number with commas as thousands separators in JavaScript
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 2 years ago.
Improve this question
Team
I would like to have a RegEx (in javascript) to validate below conditions.
Value should have atleast 3 chars
First and last char should be single quote.
please advice how the regex should be?
Thanks in advance.
If you mean three chars total:
const regex = /'.+'/;
If you mean three chars plus quotes:
const regex = /'.{3,}'/;
More info:
https://javascript.info/regexp-quantifiers
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 4 years ago.
Improve this question
I cannot understand the function used below that just splits a string on '.'.
Could you help me understand why it uses the extra replace statements?
function dotSplit (str) {
return str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
.replace(/\\\./g, '\u0001')
.split(/\./).map(function (part) {
return part.replace(/\1/g, '\\.')
.replace(/\2LITERAL\\1LITERAL\2/g, '\u0001')
})
}
Here, \1 means to match the character whose octal representation in Latin-1 encoding is 1. That character is SOH, or the start of heading character. What it does above is replace all occurrences of that with \u0002LITERAL\\1LITERAL\u0002, where \u2002 stands for the character STX(Start of text).
You can try it here:
https://regex101.com/r/n9LaJY/1
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