-2

I have a regular expression please check the corresponding regexr link http://regexr.com/3brm6 and it is working fine as per my requirement. While using the same regular expression over javascript it is not working. below is the code

function validateAddresscode(Addr) {
    var addrRegExp = /^([pP]{1,1}[\.]?[oO]{1,1}[\.]?)$/igm;         
    if (addrRegExp.test(Addr)) {
        return true;
    } else {
        return false;
    }
}

Please help me out thanks in advance.

  • I tried that also and still then it is not working /^([pP]{1,1}[\.]?[oO]{1,1}[\.]?)$/igm – MADAN PATRO Sep 24 '15 at 08:03
  • 2
    post the content of variable `Addr` – Avinash Raj Sep 24 '15 at 08:03
  • if (!validateAddresscode(AddressLine1)) { $("#AddressLine1").addClass('error'); alert('We are unable to ship to PO boxes'); } else $("#AddressLine1").removeClass('error'); – MADAN PATRO Sep 24 '15 at 08:06
  • What is your input address? – xxxmatko Sep 24 '15 at 08:06
  • Have you got console errors? – Marcos Pérez Gude Sep 24 '15 at 08:07
  • the contents will be same as the string present in regexr links – MADAN PATRO Sep 24 '15 at 08:08
  • no console errors. Input address address can be any text but if it contains PO or P.O. it will show error however if it is present inside a word it should not show error. – MADAN PATRO Sep 24 '15 at 08:12
  • @stribizhev your regular expression is showing javascript error i think you have missed "(" plz let me know where i need to insert it – MADAN PATRO Sep 24 '15 at 08:15
  • no it is not working for p.o or po but if po is present inside a word it is showing error which it should not means it is working the reverse way – MADAN PATRO Sep 24 '15 at 08:19
  • Wait, you cannot use `test` with `g`. Remove the `/g` modifier. Does it work now? If it does not, please post real text your are passing to the function, and what you expect the code to do (i.e. explain the expected result). – Wiktor Stribiżew Sep 24 '15 at 08:23
  • no it still doesn't work. I am first passing po if it shows error then it is working fine then i am passing p.o if it is showing error then it is fine then i will pass po inside a word like portal and this time it should not show error. so this is my requirement hope i am clear with it – MADAN PATRO Sep 24 '15 at 08:31
  • Oh, then you try to show error when the `po` or `p.o`... are not the whole string? Use `var addrRegExp = /^p\.?o\.?)$/i; if (!addrRegExp.test(Addr)) { return true; } else { return false; }` – Wiktor Stribiżew Sep 24 '15 at 08:39
  • thanks it is solved now – MADAN PATRO Sep 24 '15 at 08:44

1 Answers1

1

You are looking for a way to show error when a whole string is equal to p.o., P.O, etc.

You need to fix the logic here, and trim your regex a bit since {1,1} is redundant, as well as the character classes [oO] (since you are using /i case-insensitive modifier, you can just use o):

var addrRegExp = /^p\.?o\.?$/i;
if (!addrRegExp.test(Addr)) {
   return true; 
} else { 
   return false; 
} 

And remember to never use /g with RegExp#test method.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Beat me to it, I wrote the exact same regex [here](http://regexr.com/3brre) with a capture group lol – SidOfc Sep 24 '15 at 08:56
  • 1
    @SidneyLiebrand: The regex is basic here, the problem is to make it fit the logic. BTW, you do not really need a capturing group here. And in general, I think only in Perl it is OK to use a capturing group around the whole pattern to retrieve the whole match value (due to issues with `$&` back-reference). I have not heard of any performance issues in JS when using this back-reference, so I would use that if I had to retrieve the match value. But it is just not used in the code with `test()`. – Wiktor Stribiżew Sep 24 '15 at 09:00
  • You are correct about the capture group since you don't need the match at all, It's a bad habit of mine but it does help seeing the actual pattern or the individual 'pieces' of the pattern sometimes (obviously when the regex is way more complex than this ;)) – SidOfc Sep 24 '15 at 09:10
  • @MADANPATRO: Please consider accepting the answer since you say it works for you. – Wiktor Stribiżew Sep 24 '15 at 23:22