Reputation: 65
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
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:
^
and trailing $
mean that the overall pattern has to match the entire test string. (That's the same as the Java version.)(?: )
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).(?! )
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.*
, meaning that the matcher should try over and over again to match each character to the end of the test string.Upvotes: 2