Detecting POboxes (form validation) [duplicate] - javascript

This question already has answers here:
Jquery PO BOX validation
(2 answers)
PO Box Regular Expression Validation
(15 answers)
Closed 9 years ago.
I have a function from another thread that helps to detect POBoxes, but it doesn't quite work as intended.
function isPOBox(v){
var r = new RegExp('[PDO.]*\\s?B(ox)?.*\\d+', 'i');
return v.match(r);
}
If I have the value 'Lvl 1 TowerB, 999 C G Road' it incorrectly picks it up as a PObox.
As you can see, there's no P in the above.
How would I go about editing the regex to be more specific around POBoxes?
I have set up a demo Fiddle here: http://jsfiddle.net/xCQwM/

If you look at the actual match:
> "Lvl 1 TowerB, 999 C G Road".match(new RegExp('[PDO.]*\\s?B(ox)?.*\\d+',"i"))
[ 'B, 999',
undefined,
index: 11,
input: 'Lvl 1 TowerB, 999 C G Road' ]
That is a match because:
[PDO.]\* indicates that the first part of the match is optional
\\s? is optional
(ox)? is optional
.* is optional
One set of strings that will match your regex is:
"B" followed by any number of characters followed by a digit
In your example, the match looks like
"B" matches "B"
"," matches ".*"
"999" matches "\\d+"
You need to give more details regarding what you expect a P.O. Box to look like in order for us to give a better regex

The answer to your question, as you have currently worded it, is to replace [PDO.]* with [PDO.]+ so it matches at least one char. You might want to use ([PDO]\\.){2} though.
I'm thinking something like this might be better:
([PDO]\\.){2}\\s?B(ox|\\.)\\s?\\d+

Related

Regex pattern for //;\n2;3;4 [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 1 year ago.
I am trying to find a regex which supports a pattern like below:
String starts with //
String has a delimiter after //; (suppose ; is the delimiter)
String has \n after demiliter (//;\n)
Finally String contains any number of digits with that delimiter (//;\n2;3;4;5)
Could you help?
I tried ^//\\D+\\n.*$ but it doesn't work.
Thanks in advance!
Sample: //;\n2;3;4;5
Answer: [/]{2}[;]\\[n](\d[;]){1,999}\d
This will allow further combinations of a decimal followed by ;
\d is added at the end in the case a semicolon is not added judging by your sample
Okay based on your additional comment this could work. It's very messy but it may just get the job done.
var string = "//;\n2;3;4;5";
console.log(
string.replace(/[^0-9,.]+/g," ").trim().split(" ").map(function(x){return parseInt(x, 10);}).reduce(function(a, b){return a + b;}, 0)
);
Console log results in 14

Java script regular expression a bit confusing [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I am actually new to javascript and I'm trying to understand what went wrong with this code.
I have a function that accepts a abc as a parameter.
This regular expression was given to me by one of my colleges. I don't have any idea what it's doing.
Just wanted to understand what is the return statement here.
(function(abc) {
var match = abc.match(/(\d+).+?(\d+)/);
return +match[2] + 1;
});
I think the match will contain digits in decimal format but not clear about it.
what will this return? Please let me understand this, will be a great help.
(\d+) - one or more digits (0-9)
.+? - one or more periods (.)
(\d+) - one or more digits (0-9)
Debuggex Demo
You can easily create a snippet and debug it. Using provided example:
function getDiskInfo(diskinfo) {
var match = diskinfo.match(/(\d+).+?(\d+)/);
return +match[2] + 1;
}
console.log(getDiskInfo('111.222'));
In this example, as described by #phuzi:
var match = ['111.222', '111', '222'];
After that your return statement cast your element with index = 2 to Number and increments it by one. So using my example the final result will be 223.

Regex gives different result in javascript and .NET [duplicate]

This question already has an answer here:
Convert JavaScript Regex to C#
(1 answer)
Closed 4 years ago.
I need a regex which matches pattern name1\name2 with the restriction that name1 must not contain some special characters such as < , >. name1, name2 can have spaces.
I am using this regex and it seems to work fine in java script :
/^[^ &<>;]+\\./
In my C sharp code, I am using the regex below:
var pattern= #"^[^ &<>;]+\\.";
The C sharp results fail for the input : 8 [ } \ ;
where as it passes for javascript.
How can I get similar results ?
Problem with the example that you've given, you ommited space from list of allowed characters. For your example this pattern works:
var pattern = #"^[^&<>;]+\\.";

How to write (A or B or C) followed by X in JavaScript regex? [duplicate]

This question already has answers here:
Regex to extract substring, returning 2 results for some reason
(5 answers)
Closed 6 years ago.
I would like to match a string on the format:
"XYZ 1234.10"
//XYZ can be one of a relatively large, but defined, set of 3 uppercase letter combination.
//String must start with XYZ or other from allowed set
//1234 can be an integer of arbitrary length >= 0
//10 can be an arbitrary integer in the range 0-99, but must have string-length of 2. (eg. 00, 03, 82)
//String must end with .NN (dot, number, number) /\.\d\d$/
I have tried the following regex, but captures "XYZ" as well as "XYZ 1234.10":
var regex = /^(XYZ|ABC|QST) \d+\.\d\d$/
regex.exec("XYZ 1234.10")
// => ["XYZ 1234.10", "XYZ"] would like it to be ["XYZ 1234.10"]
I would like to get a match only on the full expression. I have not been able to find a way to express this with javascript regex.
Is this achievable at all? If so: how can it be done?
// => ["XYZ 1234.10", "XYZ"] would like it to be ["XYZ 1234.10"]
exec is giving you the overall match followed by the contents of the capture groups. If you don't want the group to capture, make it a non-capturing group:
var regex = /^(?:XYZ|ABC|QST) \d+\.\d\d$/;
// ------------^^
console.log(regex.exec("XYZ 1234.10"));
You are mixing match and capture group. See output with a non capturing group:
var regex = /^(?:XYZ|ABC|QST) \d+\.\d\d$/;
console.log(regex.exec("XYZ 1234.10"));

How do I properly use RegExp in JavaScript and PHP? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How do I properly use RegExp?
var text = "here come dat boi o shit waddup";
var exmaple = /[a-zA-Z0-9 ]/; // allowes a-zA-Z0-9 and whitespaces but nothing else right?
example.test(test); // would return true right?
text = "%coconut$ยง=";
example.test(text); // would return false right?
//I know this is very basic - I started learnig all this about week ago
Are JS RegExp's the same as PHP RegExp's?
How do I define banned characters instead of defining allowed characters?
How do I make it so that the var text has to contain 3 (or more) numbers/letters?
How do I include / or ",'$ etc. in my pattern?
No.
Use ^ character (i.e. [^abc] will exclude a, b and c)
Use [A-Za-z]{3} for letters and \d{3} for digits. If you want 3 or more, use \d{3,}
Use escape character (\/, \', \", '\$')

Categories

Resources