user3017924
user3017924

Reputation: 65

Convert Java Pattern to Javascript Pattern

I have following java pattern.

^[ -~&&[^"'<>\\]]*$

Basically this is everything from space to ~ character (from ascii table) excluding double quotes, single quotes, angular brackets and backslash.

I would like to convert it into Javascript pattern, I would appreciate any help.

Upvotes: 0

Views: 187

Answers (1)

Pointy
Pointy

Reputation: 413709

The only way I can think of to do that is with negative lookahead:

var pattern = /^(?:(?!["'<>\\])[ -~])*$/;

The negative lookahead (?!["'<>\\]) will cause the match to fail if it matches one of the characters you don't want.

If you want to keep the same pattern for both languages, then this one should work in Java too.

edit — breaking it down:

  • The leading ^ and trailing $ mean that the overall pattern has to match the entire test string. (That's the same as the Java version.)
  • The outer (?: ) grouping is called a "non-capturing" group. An ordinary group made with plain parentheses would work too, but I am trying to get in the habit of using non-capturing groups when I don't need to do the capture part. Probably not an issue either way. However the point of it is that we need to group together the following two parts so that the * operator can apply (more below).
  • The (?! ) part is the negative lookahead. What that does is tell the matcher to see whether the pattern in the lookahead matches, but to do so without "advancing" through the pattern. It's like a "peek around the corner" tool. Because it's negative lookahead, if the pattern does match, then the lookahead fails. This prevents the pattern from matching the punctuation characters excluded in the Java version.
  • After the lookahead is the "all 7-bit ASCII characters" pattern from the Java version, minus the conjunctive subclause (which doesn't work in JavaScript).
  • The combination of the negative lookahead and the "any character" pattern are grouped with *, meaning that the matcher should try over and over again to match each character to the end of the test string.

Upvotes: 2

Related Questions