javascript regular expression
-
I have this reg expression on javascript that checks for poboxes. how can I chnage it to map only for starting string and not anyqwhere on the string b/c if address is seAPOrt then it treats as a po box: var poboxPattern = new RegExp("(APO|A\.P\.O\.|A P O|FPO|F\.P\.O\.|F P O|POB|P\.O\.B\.|POBOX|P\.O\.BOX|PO BOX|P O BOX|P\.O\. BOX|POST OFFICE)"); Please help
-
I have this reg expression on javascript that checks for poboxes. how can I chnage it to map only for starting string and not anyqwhere on the string b/c if address is seAPOrt then it treats as a po box: var poboxPattern = new RegExp("(APO|A\.P\.O\.|A P O|FPO|F\.P\.O\.|F P O|POB|P\.O\.B\.|POBOX|P\.O\.BOX|PO BOX|P O BOX|P\.O\. BOX|POST OFFICE)"); Please help
-
Put ^ at the start of the pattern to match the start of the string.
Despite everything, the person most likely to be fooling you next is yourself.
-
The string literal actually looks exactly the same. In the Javascript you need to escape the backslash characters with a backslash character:
var poboxPattern = new RegExp("^(APO|A\\.P\\.O\\.|A P O|FPO|F\\.P\\.O\\.|F P O|POB|P\\.O\\.B\\.|POBOX|P\\.O\\.BOX|PO BOX|P O BOX|P\\.O\\. BOX|POST OFFICE)");
In C#:RegEx postboxExpression = new RegEx("^(APO|A\\.P\\.O\\.|A P O|FPO|F\\.P\\.O\\.|F P O|POB|P\\.O\\.B\\.|POBOX|P\\.O\\.BOX|PO BOX|P O BOX|P\\.O\\. BOX|POST OFFICE)");
However, in C# you can also use a @ delimited string so that you don't need to escape the backslashes:RegEx postboxExpression = new RegEx(@"^(APO|A\.P\.O\.|A P O|FPO|F\.P\.O\.|F P O|POB|P\.O\.B\.|POBOX|P\.O\.BOX|PO BOX|P O BOX|P\.O\. BOX|POST OFFICE)");
Despite everything, the person most likely to be fooling you next is yourself.