This question already has answers here:
Regular expression to allow spaces between words
(13 answers)
Closed 3 years ago.
I have this phone regex, but I want it to accept spaces.
For example +57 52 5252255 should pass, but currently it's not passing.
also "+91 9 820 09 8200" should pass
so a space anywhere is acceptable
var phone_regex =/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
https://jsfiddle.net/ofn9knay/268/
In order to match that string, your second capturing group needs to accept 2 or 3 digits.
pattern = /^\s*(?:\+?(\d{1,3}))?[- (]*(\d{2,3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
test_strings = [
"+57 52 5252255",
"9820098200#301",
"+919820098200",
"+91-982-009-8200",
"+1 (866) 582-2655",
"+91 444 741 4000",
];
for (var i = 0; i < test_strings.length; i++) {
console.log(test_strings[i] + ": " + pattern.test(test_strings[i]));
}
We could try to tweak your existing complicated regex, but assuming you already have the phone number as an input in a variable, then one simple thing to do would be to just remove all whitespace:
var phone_regex =/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/;
var phone = "+91 9 820 09 8200";
phone = phone.replace(/ /g,'');
console.log(phone_regex.test(phone));
Then, just use your current regex as you were doing.
Related
This question already has answers here:
Enhancing regex of thousands separator?
(5 answers)
Closed 10 months ago.
I need a regular expression for adding a dot as a thousand and millon separator in Javascript. I've searched the whole web without finding the format I need.
It would return this:
1
12
123
1.234
12.345
123.456
1.234.567
12.345.678
123.456.789
I'm trying to use it in an input.
handleIdChange = (personalIdNumber) => {
this.validator.updateField("personalId", {
number: personalIdNumber
})
this.props.setVisitorField({
field: "personalId.number",
value: personalIdNumber
});
<CLTextInput
highContrast={true}
onChangeText={this.handleIdChange}
keyboardType="numeric"
placeholder="NĂºmero"
returnKeyType="next"
autoCapitalize="none"
autoCorrect={false}
value={visitor.personalId.number}
error={this.shouldShowError("personalId")}
accessibility
/>
Don't use regular expressions to format numbers. Use a proper number formatter.
If you want german-style number formatting, set that as the locale.
const formatter = new Intl.NumberFormat('de-DE');
for (let i = 0; i < 10; i++) {
const number = 10 ** i;
console.log(formatter.format(number));
}
This question already has an answer here:
Multiply regex matches and replace them in string
(1 answer)
Closed 2 years ago.
For example,
x: My shop of 23 items came to 54
turns into
My shop of 46 items came to 108
where x is a string
Just use replace() with regular expression:
const string = 'My shop of 23 items came to 54';
const doubled = string.replace(/\d+/g, n => n * 2);
// My shop of 46 items came to 108
You can use a custom replace function along with a regex to find the items you want to replace, like so:
const string = `My shop of 23 items came to 54`;
const out = string.replace(/\d+/g, function() {
return parseInt(arguments[0]) * 2;
});
console.log(out);
This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Closed 5 years ago.
I have a number, for example:
25297710.1088
I need to add a bit between them and leave two characters after the point.
For example:
25 297 710.10
While I stopped at this:
$(td).text().reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse());
String.prototype.reverse = function() {
return this.split('').reverse().join('');
}
From this code I get the following:
25 297 710.1 088
Where $(td).text() I get a number from the cell of the row in the table.
If I have numbers, for example:
25297710.10
then i get:
25 297 710.10
It's ok.
What I need to do to leave two characters after the point?
You can use a RegExp to format the number/string. The input is converted to string using the relevant toString method.
function formatNumber(input) {
return input.toString().replace(/\d*(\d{2})(\d{3})(\d{3})\.(\d{2})\d*$/, "$1 $2 $3.$4");
}
var str = "25297710.1088";
var num1 = 25297710.1088;
var num2 = 2545454545454.2254;
var num3 = 232545454511112.3354122313123123;
console.log(formatNumber(str));
console.log(formatNumber(num1));
console.log(formatNumber(num2));
console.log(formatNumber(num3));
I think you can do next steps:
1) you have 25 297 710.10
2) you find position of dot symbol ->#pos
3) you replace bits in string in range between #pos and end of your string
4) you cut string after dot to 2 characters
This question already has answers here:
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
(10 answers)
Closed 5 years ago.
I know this has been asked a million times, but I just can't seem to crack it.
I have this:
function checkPassword(strPassword)
{
var objPattern = new RegExp("^.*(?=.{6,})(?=.*[a-z])[a-z0-9]*$");
var blnResult = objPattern.test(strPassword);
return(blnResult)
}
...but it only seems to check the length, and not if there's a number?
What have I missed?
Edit:
The number can be anywhere in the string, not necessarily at the end.
Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable
If you need exactly 6 characters plus 1 number then you can use ^[A-z]{6}[0-9]{1}$ or like atleast 6 characters and atleast 1 number then use ^[A-z]{6,}[0-9]{1,}$
You can just include both tests separately in your function:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
Demo:
function checkPassword(strPassword){
var blnResult = /\w{6,}/.test(strPassword)
&& /\d+/.test(strPassword);
return(blnResult)
}
var passwords = ["zeaezee2reer", "sds2", "ssdsdsdsdsd", "12155"];
passwords.forEach(function(p){
console.log(p+" ::: "+ checkPassword(p));
});
This question already has answers here:
Natural sort of alphanumerical strings in JavaScript
(6 answers)
Closed 7 years ago.
I need to sort a list that's alphanumeric, but it's botching the multi-digit
integers since it's character-by-character and 1 is less than 8.
Any it's particularly tricky because there can be alphabetic characters before and after the numbers. So if it can somehow understand the full integer instead of just the single digit then that would do it.
Normal Sort
Grade 10 Academic
Grade 10 Applied
Grade 11
Grade 8
Grade 9
Desired Sort:
Grade 8
Grade 9
Grade 10 Academic
Grade 10 Applied
Grade 11
Anyone know how to make a Javascript script to sort this way? Or even better if you have an AngularJS solution since I'm using that.
Thanks in advance for any help you can offer.
var arr = [
"Grade 10 Applied",
"Grade 10 Academic",
"Grade 11",
"Grade 11 Testy",
"Grade 9",
"Grade 8"
];
alert(arr.sort(function(a, b) {
// split the strings into arrays of words
var aParts = a.split(' ');
var bParts = b.split(' ');
// compare the corresponding words, if they are integers parseInt first
for(var i=0; i < Math.min(aParts.length, bParts.length); i++) {
var aPart = /\d+/.test(aParts[i]) ? parseInt(aParts[i]) : aParts[i];
var bPart = /\d+/.test(bParts[i]) ? parseInt(bParts[i]) : bParts[i];
if(aPart === bPart) { continue; }
return aPart < bPart ? -1 : 1;
}
// fall back to using the array's length
// ["dog", "cat"] < ["dog", "cat", "frog"]
return aParts.length - bParts.length
}).join('\n'));