开发者

is it possible to match these 4 modes of value assignation in regex?

in many languages there can be many possibilities to assing a string to a variable:

var = "some 'quoted' string"
var = 'some "quoted" string'
var = `some 'quoted "quoted" string`
var = somestring

Of course in this last variant the space is not possible and end of string is marked by some special character like ; space or > in html.

But my question regards possibility to match all these 4 situat开发者_StackOverflow中文版ions with one regex. The worse situation is with these quotes where first character must be searched at the end of a string and with exception of escaping it.


var = (?:([`'"])(?:(?!\1).)*\1|[^\s;>]*$)

works for your examples. If you also want to handle escaped quotes, then try

var = (?:([`'"])(?:(?!\1)(?:\\.|.))*\1|[^\s;>]*$)

As a verbose regex:

var\s*=\s*
(?:      # match either:...
 ([`'"]) # one of the quote characters, then...
 (?:     # match the following any number of times:
  (?!\1) # first assert that the next character isn't the quote we matched earlier
  (?:    # if so, then match either
   \\.   # an escaped character
   |     # or
   .     # an unescaped character
  )
 )*      # repeat as often as needed
 \1      # then match the opening quote character again
 |       # ...or...
 [^\s;>]* #match any suite of characters except whitespace, ; or > up to...
 $       # the end of the line/string
)


The easiest would be to use an alternation and describe each format separately:

var = ("[^"]*"|'[^']*'|`[^`]*`|[^;\s>]*)

And if you want to allow that each delimiter may be used when escaped, add that case as follows:

var = ("([^\\"]|\\")*"|'([^\\']|\\')*'|`([^\\`]|\\`)*`|[^;\s>]*)

And if you want to allow other characters (or even any character) to be escaped, replace the corresponding escape sequence with a character class containing the characters \\[…] or use \\. for any character.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜