我正在尝试编写一个在短代码属性的双引号内清除双引号的正则表达式。
我写了这个正则表达式
\="(.*?)\"
并匹配引号http://regex101.com/r/jW0uC4
之间的字符串但是当我的属性值也包含双引号时,它会失败http://regex101.com/r/pL9bI0
那么,我怎样才能改进正则表达式,因为它只会在="之间捕获字符串。最后"
提前致谢
答案 0 :(得分:0)
此正则表达式与您提供的示例文本匹配:
/="(.*?)"(?=\s*(?:[a-z]+=|]))/
说明:
=" '="'
( group and capture to \1:
.*? any character except \n (0 or more times
(matching the least amount possible))
) end of \1
" '"'
(?= look ahead to see if there is:
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
(?: group, but do not capture:
[a-z]+ any character of: 'a' to 'z' (1 or
more times (matching the most amount
possible))
= '='
| OR
] ']'
) end of grouping
) end of look-ahead
但是用户错误很难修复,并且此正则表达式可能无法在所有情况下都有效(例如,如果文本包含=
个字符)。您应确保正确转义用户输入。