I have a regular expression that allows you to arrange it in bracket groups, but there, as I need a maximum of 15 characters, and the value of the code is calculated dynamically, I would like to know if it is possible to somehow limit the size of the characters in the regular expression to 15?
My regexp
let code = 3 // can be 1,2,3,4 etc
someValue
.replace(/(?<!^)\+|[^\d+]+/g, '')
.replace(new RegExp(`(\\d{${code}})(\\d)`), '($1) $2')
.replace(/(\d{3})(\d)/, '$1-$2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(-\d)/, '$1')
input
'123456789101112131415'
output
'(123)-456-7891-01112131415'
expected output ( max 15 digits )
'(123)-456-7891-01112'
Nit:
Perhaps if you have a more elegant solution to achieve this result instead of what I provided, that would be great
Related
I am trying to solve 'JavaScript Algorithms and Data Structures Projects: Telephone Number Validator' #freeCodeCamp.
I need to test if string contains 10 digits and what I've come up with returns false and I don't understand why.
console.log(/\d{10}/g.test("555-555-5555"))
If you want to do this with a single regular expression, you can use:
console.log(/^(?:\D*\d){10}\D*$/g.test("555-555-5555"))
console.log(/^(?:\D*\d){10}\D*$/g.test("555-555-55555"))
requiring the input to be composed of exactly 10 digits in addition to any number of other non-digit characters - but replacing non-digits with the empty string first would be a more intuitive and readable solution.
Here \d{10} means ten consecutive digits, not "ten digits with whatever in the middle, that's cool".
If want just 10 digits, you may want to strip non-digit data first:
let number = "555-555-5555";
// Remove all non-digit values (\D) which leaves only digits
let digits = number.replace(/\D/g, '').length;
It seems that your idea is correct, but this specific challenge has so many options for dashes [-] and parentheses [()] as inputs that there is a more efficient way to pass this.
function telephoneCheck(str) {
let phoneRegex = /^(1\s?)?(\d{3}|\(\d{3}\))[\s\-]?\d{3}[\s\-]?\d{4}$/
return phoneRegex.test(str);
}
The above is a way to complete the challenge in a single line of Regex, which can save you (or anyone else reading this in the future) a lot of time and space! Cheers
This is not a duplicate, the linked thread does not explain how to achieve this.
I'm looking to get a phone number in a specific format.
+xx (x) xxx xxx xxxx
Country code.
Space.
Zero in brackets.
Space.
3 digits.
Space.
3 digits.
Space.
4 digits.
The user could type anything in (but should always be a +61 number). So far I have tried the below.
Removing spaces and non numeric characters.
If starting with a zero, remove.
If starting with 610, remove.
If starting with 61, remove.
Re add country code in specific format and format rest of phone number is a 3,3,4 format.
My question, is - is there a way to simply the below to perhaps one expression?
value = value.replace(/\D/g,'');
value = value.startsWith(0) ? value.substring(1) : value;
value = value.startsWith('610') ? value.substring(3) : value;
value = value.startsWith('61') ? value.substring(2) : value;
value = '+61 (0) ' + value.replace(/\d{3,4}?(?=...)/g, '$& ');
To expand and explain on #splash58's comment they propose using two regular expressions to do the full replacement you desire. The first(/\D|(0+|610|61)/gi) will remove all unwanted characters within the string. The second (/(\d{3})(\d{3})(\d{4})/gi) will take the remaining digits and capture the desired groupings so you can format them as desired. I highly suggest looking at the regex101 links they provided as that site will fully explain how and why a given expressions matches what it does on the right.
Short version:
/\D|(0+|610|61)/gi will match any NON-digit character OR a string of 0s, "610" or "61". Replace this with nothing to remove
/(\d{3})(\d{3})(\d{4})/gi will match a string of 10 digits and capture groups, that's what the parentheses are, of 3 digits, 3 digits and 4 digits. These can be referenced in the replacement as identifiers $1, $2 and $3 according to their position.
Putting it all together:
// look in a string and return formatted phone number only
function phone(str) {
str = str.replace(/\D|(0+|610|61)/gi, '');
str = str.replace(/(\d{3})(\d{3})(\d{4})/gi, '+61 (0) $1 $2 $3');
return str;
}
console.log(phone('xgsh6101231231234vvajx'));
console.log(phone('+6101231231234'));
I would also recommend first doing a search on the entire input string for a series of numbers or whitespace so that you end up with less false positives. This can be done with a regular expression like /[\d\s]+/
You might match the number using:
^.*?\+?0*610?(\d{3})(\d{3})(\d{4})(?!\d).*$
Regex demo
And replace with:
+61 (0) $1 $2 $3
Explanation
^ Assert the start of the string
.*? Match 0+ characters non greedy
\+? Match an optional plus sign
0*610? Match 0+ times a zero, 61 with optional zero
(\d{3})(\d{3})(\d{4}) match 3 groups with 3, 3, and 4 digits
(?!\d) Negative lookahead to assert what follows is not a digit
.* Match 0+ characters
$ Assert the end of the string
const strings = [
"xgsh6101231231234vvajx",
"xgsh06101231231234vvajx",
"xgsh000006101231231234vvajx",
"+6101231231234",
"xgsh61012312312345vvajx",
"xgsh5101231231234vvajx",
"xgsh00000101231231234vvajx",
"xgsh6143545626455345601231231234vvajx"
];
let pattern = /^.*?\+?0*610?(\d{3})(\d{3})(\d{4})(?!\d).*$/;
strings.forEach((s) => {
console.log(s.replace(pattern, "+61 (0) $1 $2 $3"));
});
I'm trying to know how many digits there is in a string that is essentially like a password.
For now I have this regex :
^(?=.*[0-9]{3,})([a-zA-Z0-9_/+*.-]{6,})$
It works great when their is 3 digits in a row but not when they are separated in the whole string.
I need to be able to know if there is 3 digit in strings like those :
h123dasd
1hkh/23jd
1gvbn/*2fefse-
What can I do ?
You can use this regex:
/^(?=(?:\D*\d){3,})[a-zA-Z0-9_/+*.-]{6,}$/
This will enforce 3 digits in your input that may or may not be consecutive.
RegEx Demo
No need for such a complicated regex IMO - just extract digits from the string, concat the matches, and then check the length. Something like:
str.match(/\d+/g).reduce((p, c) => p + c).length > 3;
DEMO
Suppose I have some text which I want to extract only the digits from, and then apply a formatting filter to it.
For example,
//extract only the digits
TheText = TheText.replace(/\D/g, '');
//apply formatting pattern
TheText = TheText.replace(/(\d{1})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?/, '$1 $2 $3 $4 $5')
Basically, the output will look like this:
1 23 45 67 89
How can I combine these regular expressions into just one?
Thanks.
Note: I'm not looking to chain the statements using .replace(...).replace(...) statements, just the regex.
Least clever way: extract each digit separately.
.replace(/\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*/,
"$1 $2$3 $4$5 $6$7 $8$9")
I'm using this /[-\+,\.0-9]+/ to match numbers in strings like +4400,00 % or -3500,00 % or 0.00 %.
The matched results I want is +4400,00 and I correctly get it.
What if I wanted the same results for a string like +4.400,00 % (dot for thousands) ?
EDIT
How do I have to modify my RegEx for matching numbers in strings like <font color="red">+44.500 %</font>?
/[\-\+]?\s*[0-9]{1,3}(\.[0-9]{3})*,[0-9]+/
That should cover strings that
may start with a + or -, and then perhaps some whitespaces
then have between one and three numbers
then have groups of three numbers, prefixed with a period
then have a comma and at least one number behind the comma
Regarding your additional question (matching numbers inside strings), you should look into the manual of whatever regex API you're using. Most APIs have separate search and match methods; match wants the whole string to be part of your regular expression's language, while search will also match substrings.
[\+-]? - plus or minus
\d{1,3} - some digits
(\.\d{3})* - groups of 3 digits with point before
,\d{2} comma and 2 more digits
And so we get:
/[+-]?\d{1,3}(\.\d{3})*,\d{2}/
Your regex will already match ".". But it sounds like you also want to strip "." out? if that's the case, you need a substiution. In Perl,
if ($input =~ /(-|\+)[0-9][,\.0-9]+/) {
$input =~ s/\.//;
} else {
die;
}
I've also changed the regex so it will only match - and + at the start, and so it requires an initial digit