我有以下可能性:
' !This is a string! '
'!This is a string !'
'! This is a string !'
' ! This is a string! '
' ! This is a string '
在所有这些情况下,我都想匹配
'This is a string'
到目前为止,我一直在尝试以下方法:
/\s*!(.*)!{0,1}
/\s*!(.*?)!{0,1}
但是它要么吞噬了
!
要么根本不匹配。请注意,结尾处的!
是可选的。Here's a fiddle to see what I mean
最佳答案
也许这一个
/^\s*!\s*(.*?)\s*!?\s*$/
关于javascript - 正则表达式略有贪婪捕获?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8632389/