Regex that exclude specific pattern of ip - javascript

I am filtering specific ip address via regex. For example,
all the ip address containing 1 in the last octet i.e. XXX.XXX.XXX.1 should be excluded but not 11 or 123 or 125. Sample example:
the regex should discard
192.168.1.1
192.168.20.1
192.168.30.1
but should not discard
192.168.1.101
192.168.20.103
I have tried the regex as :
^(?!\d{1,3}\.\d{1,3}\.\d{1,3}\.[^\1]).*$
but could not exclude as expected.
Any help is appreciated !!!

When matching the final octet, use (?:1\d+|[02-9]\d*)$ - either match a 1 which is followed by other digits, or match something which isn't a 1, possibly followed by other digits:
^\d{1,3}(?:\.\d{1,3}){2}\.(?:1\d+|[02-9]\d{0,2})$
https://regex101.com/r/gB5c6z/1
Another option, with negative lookahead:
^\d{1,3}(?:\.\d{1,3}){2}\.(?!1$)\d{1,3}$

considering given format and ip's will be valid you can use endsWith
let ips = [`192.168.1.1`, `192.168.20.1`, `192.168.30.1`, `192.168.1.101`, `192.168.20.103`]
let checker = (str) => !str.endsWith(`.1`)
ips.forEach(v => console.log(checker(v)))

Related

Generate regex to match with the url

I have a string want those product hubs vendor matched properly with their respective position and digit between them must digit but.digits can vary in numbers.
pattern: /products/8236/hubs/1/vendor
url="https://d3skctyxg9sfpd.cloudfront.net/sadassdsad/products/8236/hubs/1/vendor"
this.tests = "products/8236/hubs/1/vendor";
var fields = this.tests.split('_qa_current');
console.log(fields, "fielding values fetched");
/products/.test(fields);
console.log(/products/.test(this.tests), "url getting");
/^products\[0-9]\\hubs\[0-9]\\vendors$/.test(fields[1]);
This should be your regex
^\/products\/\d+\/hubs\/\d+\/vendor$
The slashes are not escaped correctly in your regex.
Code
this.tests = "https://d3skctyxg9sfpd.cloudfront.net/milkbasket_qa_current/products/8236/hubs/1/vendor";
var fields = this.tests.split('_qa_current');
console.log(fields, "fielding values fetched");
/products/.test(fields);
console.log(/products/.test(this.tests), "url getting");
/^\/products\/\d+\/hubs\/\d+\/vendor$/.test(fields[1]);
Regex: /-\/products\/\d+\/hubs\/\d+\/vendor/g
You can replace the digit with the \d+ meaning there can be one or more numbers in this place.
Here's the website to try the pattern and see for yourself: https://regexr.com/.
There are also explanations on the bottom of the screen to help you understand better what the rules do.

Validation Regex for quantities with centesimal and without centesimal ...?

Hello to the community I have a query, I need a validation Regex, for amounts without decimals, that consider valid the following structure.
99,999,999
If I add a value:
12345678 -> Ok
12,345,678 -> Ok
123,456,789 -> Failed
123,45,6,78 -> Failed
12,345,678.50 -> Failed
12,456,7ab -> Failed
I have only been able to validate the size of 8 numerical characters:
var regex8 = /^-?([0-9]{1,8})?$/;
I wait for your comments.
Thank you.
With a bit of work you can craft a pattern to do this:
https://regex101.com/r/DKpSUR/1
/^-?([0-9]{1,2},?)?([0-9]{3},?){1,2}$/
This regex should supply you with what you want or point you in a direction:
-?([0-9]{1,2},)?([0-9]{3},)?[0-9]{3}
This will match an optional leading sign [-+]?
followed by either
a string of one or more digits \d+ or
a 1-3 digit string \d{1,3} followed by one more more groups of comma-3-digits ,\d{3}
Putting it all together:
/^[-+]?((\d+)|(\d{1,3}(,\d{3})+))$/
I have grouped them with parentheses () to make it clear, but be aware this creates capturing groups.
var rgx = /^[-+]?((\d+)|(\d{1,3}(,\d{3})+))$/
var matched = "+813,823".match(rgx); // ==> ["+813,823", "813,823", undefined, "813,823", ",823"]
You would want matched[0] to get the whole match.

Number extraction from string using regex working partially

I'm extracting the phone numbers that begin with 9 followed by other 9 digits from tweets using JavaScript.
Here's the regex pattern I am using:
var numberPattern = /^9[0-9]{9}/;
Here's the pattern matching phase:
var numberstring = JSON.stringify(data[i].text);
if(numberPattern.test(data[i].text.toString()) == true){
var obj={
tweet : {
status : data[i].text
},
phone : numberstring.match(numberPattern)
}
//console.log(numberstring.match(numberPattern));
stringarray.push(obj);
The problem is it is working for few numbers and not all. Also, I want to modify the regex to accept +91 prefix to numbers as well and(or) reject a starting 0 in numbers. I'm a beginner in regex, so help is needed. Thanks.
Example:
#Chennai O-ve blood for #arun_scribbles 's friend's father surgery in few days. Pl call 9445866298. 15May. via #arun_scribbles
Your regex pattern seems to be designed to allow a 9 or 8 at the beginning, but it would be better to enclose that choice in parentheses: /^(9|8)[0-9]{9}/.
To allow an optional "+" at the beginning, follow it with a question mark to make it optional: /^\+?(9|8)[0-9]{9}/.
To allow any character except "0", replace the (9|8) with a construct to accept only 1-9: /^\+?[1-9][0-9]{9}/.
And in your example, the phone number doesn't come at the beginning of the line, so the caret will not find it. If you're looking for content in the middle of the line, you'll need to drop the caret: /\+?[1-9][0-9]{9}/.
var numberPattern = /([+]91)?9[0-9]{9}\b/;
Try this regex pattern: [\+]?[0-9]{1,4}[\s]?[0-9]{10}
It accepts any country code with +, a space, and then 10 digit number.

Match a string between two other strings with regex in javascript

How can I use regex in javascript to match the phone number and only the phone number in the sample string below? The way I have it written below matches "PHONE=9878906756", I need it to only match "9878906756". I think this should be relatively simple, but I've tried putting negating like characters around "PHONE=" with no luck. I can get the phone number in its own group, but that doesn't help when assigning to the javascript var, which only cares what matches.
REGEX:
/PHONE=([^,]*)/g
DATA:
3={STATE=, SSN=, STREET2=, STREET1=, PHONE=9878906756,
MIDDLENAME=, FIRSTNAME=Dexter, POSTALCODE=, DATEOFBIRTH=19650802,
GENDER=0, CITY=, LASTNAME=Morgan
The way you're doing it is right, you just have to get the value of the capture group rather than the value of the whole match:
var result = str.match(/PHONE=([^,]*)/); // Or result = /PHONE=([^,]*)/.exec(str);
if (result) {
console.log(result[1]); // "9878906756"
}
In the array you get back from match, the first entry is the whole match, and then there are additional entries for each capture group.
You also don't need the g flag.
Just use dataAfterRegex.substring(6) to take out the first 6 characters (i.e.: the PHONE= part).
Try
var str = "3={STATE=, SSN=, STREET2=, STREET1=, PHONE=9878906756, MIDDLENAME=, FIRSTNAME=Dexter, POSTALCODE=, DATEOFBIRTH=19650802, GENDER=0, CITY=, LASTNAME=Morgan";
var ph = str.match(/PHONE\=\d+/)[0].slice(-10);
console.log(ph);

Allowing a dash in this regex

I'm using this Wordpress plugin called 'Easy contact form' which offers standard validation methods.
It uses the following regex for phone numbers:
/^(\+{0,1}\d{1,2})*\s*(\(?\d{3}\)?\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/
Just now it allows the following formats (maybe more):
0612345678
+31612345678
But I want it to allow +316-12345678 and 06-12345678 also ... Is this possible? If so, how?
Thanks in advance!
You can use a less complex regex :
^\+?\d{2}(-?\d){8,9}$
This regex allows a + at the beginning of the phone number, then matches two digits, and after that, digits preceded (or not) by a -, for a total of 10 or 11 digits.
Now you can adapt it if the initial + is only for 11-digits phone numbers :
^\+?\d{3}(-?\d){9}|\d{2}(-?\d){8}$
My regex allow you to use - every digit. If that's an issue, it can be changed :
^\+?\d{3}(-?\d{2}){4}|\d{2}(-?\d{2}){4}$
I think this last regex will answer your needs, and it's quite simple !
When you figure out what patterns you actually want to allow and not allow. Then fill them out in this function and if the statement returns true you have your regex.
var regex = /^\+?\d{3}(-?\d{2}){4}|\d{2}(-?\d{2}){4}$/; //this is your regex, based on #Theox third answer
//allowed patterns
['0612345678', '+31612345678', '+316-12345678', '06-12345678'].every(function(test) {
return regex.exec(test) !== null;
}) &&
//disallowed patterns, none right now
[].every(function(test) {
return regex.exec(test) === null;
});

Categories

Resources