regex digits formatting - javascript

I want to use regex specifically! to check if a mobile number contains 9 or more digits. I am a little unsure as to how to format this exactly.
I want to check inside an if statement like the below
if(mob >= (.{9})
This is clearly not correct, any help would be great

Use test with the regex pattern ^\+?[0-9]{9,}$:
var number = "+123456789";
if (/^\+?[0-9]{9,}$/.test(number)) {
console.log("MATCH");
}

You need to check of numbers not for all the characters. And also use test() to check if string matches regex or not.
const testMob = str => /^\+?[0-9]{9,}$/.test(str);
console.log(testMob('+123456789')) //true
console.log(testMob('+1234567890')) //true
console.log(testMob('+133333')) //false

Related

Match all strings except master | dev

I want to create a regex that can match all strings except a set, something like:
/[^master|dev]/
basically the string would match the regex if the string wasn't the literal "master" or "dev", anyone know how? The above is pretty much completely wrong..
console.log(
/^master|dev/.test('master')
);
and so is that.
Try a negative lookahead:
/^((?!(master|dev)).)*$/
This just matches the string - if you want to match the word:
/^((?!\b(master|dev)\b).)*$/
If you're testing individual strings that you don't want to find
any of those items in, it would be something like
^(?!.*(?:master|dev)).+$
If testing the string does not exactly equal one of these, it is this
^(?!(?:master|dev)$).+$
Have you considered using logical NOT operator (!) ?
let input = "this is the input";
let excludeSet = ["master", "dev"];
let regexp = new RegExp(excludeSet.join("|"));
// if `input` does not match string in `excludeSet`
if(!regexp.test(input)) {
}

RegEx for Number and Dash/Hyphen with Certain Format

How to write JQuery/JavaScript RegEx pattern to allow only 3 numbers, 1 dash and 5 numbers OR 8 numbers?
###-##### or ######## (NO Space, NO More, NO Less and ONLY number with one dash/hyphen)
Thanks for your help.
Use .match or whatever, depending on your use case, with regex
^(\d{3}-\d{5})|\d{8}$
This conforms to your description:
\d{3}(\-)?\d{5}
And this to your example:
(\d \d{3}\-\d{5})|(\d{8})
regexPattern="[0-9]{3}[0-9-][0-9]{4}";
or
"\d{3}[\d-]\d{4}"
Tested working RegEx pattern
var pattern = /^\(?\d{3}\)?[- ]?\d{5}$/;
var val = inputs[i].value;
if(!val.match(pattern)) {
alert('Number and dash only(###-##### OR ########)');
return false;
}

Javascript timestamp formatting with regular expression?

how do i format a string of 2014-09-10 10:07:02 into something like this:
2014,09,10,10,07,02
Thanks!
Nice and simple.
var str = "2014-09-10 10:07:02";
var newstr = str.replace(/[ :-]/g, ',');
console.log(newstr);
Based on the assumption that you want to get rid of everything but the digits, an alternative is to inverse the regex to exclude everything but digits. This is, in effect, a white-listing approach as compared to the previously posted black-listing approach.
var dateTimeString = "2016-11-23 02:00:00";
var regex = /[^0-9]+/g; // Alternatively (credit zerkms): /\D+/g
var reformattedDateTimeString = dateTimeString.replace(regex, ',');
Note the + which has the effect of replacing groups of characters (e.g. two spaces would be replaced by only a single comma).
Also note that if you intend to use the strings as digits (e.g. via parseInt), numbers with a leading zero are interpreted within JavaScript as being base-8.

Regex to only allow numbers under 10 digits?

I'm trying to write a regex to verify that an input is a pure, positive whole number (up to 10 digits, but I'm applying that logic elsewhere).
Right now, this is the regex that I'm working with (which I got from here):
^(([1-9]*)|(([1-9]*).([0-9]*)))$
In this function:
if (/^(([1-9]*)|(([1-9]*).([0-9]*)))$/.test($('#targetMe').val())) {
alert('we cool')
} else {
alert('we not')
}
However, I can't seem to get it to work, and I'm not sure if it's the regex or the function. I need to disallow %, . and ' as well. I only want numeric characters. Can anyone point me in the right direction?
You can do this way:
/^[0-9]{1,10}$/
Code:
var tempVal = $('#targetMe').val();
if (/^[0-9]{1,10}$/.test(+tempVal)) // OR if (/^[0-9]{1,10}$/.test(+tempVal) && tempVal.length<=10)
alert('we cool');
else
alert('we not');
Refer LIVE DEMO
var value = $('#targetMe').val(),
re = /^[1-9][0-9]{0,8}$/;
if (re.test(value)) {
// ok
}
Would you need a regular expression?
var value = +$('#targetMe').val();
if (value && value<9999999999) { /*etc.*/ }
var reg = /^[0-9]{1,10}$/;
var checking = reg.test($('#number').val());
if(checking){
return number;
}else{
return false;
}
That's the problem with blindly copying code. The regex you copied is for numbers including floating point numbers with an arbitrary number of digits - and it is buggy, because it wouldn't allow the digit 0 before the decimal point.
You want the following regex:
^[1-9][0-9]{0,9}$
Use this regular expression to match ten digits only:
#"^\d{10}$"
To find a sequence of ten consecutive digits anywhere in a string, use:
#"\d{10}"
Note that this will also find the first 10 digits of an 11 digit number. To search anywhere in the string for exactly 10 consecutive digits.
#"(?<!\d)\d{10}(?!\d)"
check this site here you can learn JS Regular Expiration. How to create this?
https://www.regextester.com/99401

A regular expression to identify a non number string

I have a string and want to wrap non-numbers with double quotes (if they don't have them already). What is the best way to detect a non-number with a regex?
These are numbers: 123.43, 13827. These are non numbers: Hello, 2011-02-45, 20a, A23.
Here is the regex I currently have but does not handle the case where a non-number starts with a digit (so 2011-02-45 is not picked up).
str = str.replace(/(['"])?([a-zA-Z0-9_\-]+)(['"])?:/g, '"$2":');
str = str.replace(/:(['"])?([a-zA-Z_]+[a-zA-Z0-9_]*)(['"])?/g, ':"$2"');
How about this:
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
Taken from: Validate decimal numbers in JavaScript - IsNumeric()
I found the solution by reading another question. This is it:
str.replace(/(['"])?([a-zA-Z0-9_\-]*[a-zA-Z_\-]+[a-zA-Z0-9_\-]*)(['"])?/g, '"$2"');
The trick is to ensure there is a non-digit in the match.
you can do something like this, which is alot quicker than regexp!
str = +str||false;
str = 123.4 or, false when not a number
This is to typecast "str" into a real number or leave it as a string you can..
str = +str||str||false;
to take that to the next step, you can check your output:
if(typeof(str)=='string'){
//str is a string
}

Categories