0

I have following regular expression in c# (ship|sub)(?<!^(shipment|subway)$)

which is used to check if the given data contains ship or sub when the data is not shipment or subway.

I want to write the same expression in java script. I tried several options with javascript negetive look behind.. But they are not serving my purpose: for an example: when i give shipment to the regular expression, it should not return ship(as i am excluding shipment in regular expression) .. however, when i give membership, it should give ship and match result true.

Could any one of you please help me .

Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
nani
  • 37
  • 3
  • rearrange your post will be help everyone that read your post – Iswanto San Jan 28 '13 at 14:16
  • 1
    3 people voted to close this question, presumably because 90% of the question was missing, but didn't try clicking "edit" to see if something went awry. The unescaped `<` in the user's code turned out to hide most of the question. – Andrew Cheong Jan 28 '13 at 14:18
  • javascript regex parser does not support negative lookbehind. see [this question](http://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent) – jbabey Jan 28 '13 at 14:18

1 Answers1

1

Your regular expression should not have worked in .NET, either. Once you match "ship" or "sub", the negative lookbehind assertion would always be true: you matched "ship" or "sub", so the previous characters could never be "ment" or "bway", thus passing the assertion always. Instead, you must have meant:

\b(ship(?!ment)|sub(?!way))\b

This should work in Javascript as well, since it's only using negative lookahead assertions.

Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145