Convert Java Pattern to Javascript Pattern -



Convert Java Pattern to Javascript Pattern -

i have next java pattern.

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

basically space ~ character (from ascii table) excluding double quotes, single quotes, angular brackets , backslash.

i convert javascript pattern, appreciate help.

the way can think of negative lookahead:

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

the negative lookahead (?!["'<>\\]) cause match fail if matches 1 of characters don't want.

if want maintain same pattern both languages, 1 should work in java too.

edit — breaking down:

the leading ^ , trailing $ mean overall pattern has match entire test string. (that's same java version.) the outer (?: ) grouping called "non-capturing" group. ordinary grouping made plain parentheses work too, trying in habit of using non-capturing groups when don't need capture part. not issue either way. point of need grouping next 2 parts * operator can apply (more below). the (?! ) part negative lookahead. tell matcher see whether pattern in lookahead matches, without "advancing" through pattern. it's "peek around corner" tool. because it's negative lookahead, if pattern does match, lookahead fails. prevents pattern matching punctuation characters excluded in java version. after lookahead "all 7-bit ascii characters" pattern java version, minus conjunctive subclause (which doesn't work in javascript). the combination of negative lookahead , "any character" pattern grouped *, meaning matcher should seek on , on 1 time again match each character end of test string.

java javascript regex

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -