javascript - RegEx : Match a string enclosed in single quotes but don't match those inside double quotes -
i wanted write regex match strings enclosed in single quotes should not match string single quote enclosed in double quote.
example 1:
a = 'this single-quoted string'; the whole value of a should match because enclosed single quotes.
edit: exact match should be: 'this single-quoted string'
example 2:
x = "this 'string' single quote"; x should not return match because single quotes found inside double quotes.
i have tried /'.*'/g matches single quoted string inside double quoted string.
thanks help!
edit:
to make clearer
given below strings:
the "quick 'brown' fox" jumps on 'the lazy dog' near "the 'riverbank'". the match should be:
'the lazy dog'
assuming won't have deal escaped quotes (which possible make regex complicated), , quotes correctly balanced (nothing it's... "monty python's flying circus"!), single-quoted strings followed number of double quotes:
/'[^'"]*'(?=(?:[^"]*"[^"]*")*[^"]*$)/g see live on regex101.com.
explanation:
' # match ' [^'"]* # match number of characters except ' or " ' # match ' (?= # assert following regex match here: (?: # start of non-capturing group: [^"]*" # number of non-double quotes, quote. [^"]*" # same thing again, ensuring number of quotes. )* # match group number of times, including zero. [^"]* # match number of characters except " $ # until end of string. ) # (end of lookahead assertion)
Comments
Post a Comment