对键值对的棘手正则表达式解析

Tricky regex parse on key value pairs

本文关键字:正则表达式 键值对      更新时间:2023-09-26

我花了一个多小时来弄乱正则表达式模式,以便对一个复杂的字符串进行正确的find-and-replace

我需要像这样转换一个字符串:

foo a='b' c="d and e" wombat=true a fizz='buzz' "hello='goodbye'"

并将其规范化为:

foo "a='b'" "c='d and e'" "wombat='true'" a "fizz='buzz'" "hello='goodbye'"

基本上:

  • 每个key/value对都应该用双引号括起来,值用单引号括起来,无论它们以前是如何包装的。

  • 多空格值必须在前面用单引号或双引号括起来,才能作为值"包含"。


到目前为止,我正在按以下顺序使用正则表达式:

str = str.replace(/([a-zA-Z0-9]*)=("(.*?)"|'(.*?)')/g, '"$1=''$2''');

但是,这有很多问题。

是否有任何单一更换解决方案?

替换

/(['"]?)('w+)=(?:(['"])((?:(?!'3).)*)'3|('S+))'1/g

"$2='$4$5'"

给通缉犯

foo "a='b'" "c='d and e'" "wombat='true'" a "fizz='buzz'" "hello='goodbye'"

表达式细分如下:

(['"]?)            # group 1: either single or double quote, optional
('w+)              # group 2: word characters (i.e. the "key")
=                  # a literal "="
(?:                # non-capturing group
  (['"])           #   group 3: either single or double quote
  (                #   group 4 (quoted value): 
    (?:(?!'3).)*   #     any character that's not the same as group 3
  )                #   end group 4
  '3               #   the quote from group 3  
  |                #   or...
  ('S+)            #   group 5 (non-quoted value, no spaces)
)                  # end non-capturing group
'1                 # whatever group 1 had, for good measure

下面的正则表达式将满足您的要求:

"?('w+)='?(['w]+)'?"?|"?('w+)="(['w's]+)""? 

替换为:

"$1$3='$2$4'"

演示