简体   繁体   English

正则表达式在两个引号之间匹配

[英]Regex match between two quotes

Here are the types of strings I want to match 这是我要匹配的字符串类型

(string are single quotes delimited (think var str = '"hi, what"s up, what a nice day"'; )): (字符串是用单引号引起来的(想想var str = '"hi, what"s up, what a nice day"'; )):

  1. ' "This is a str"'
  2. ' "This is also a str "
  3. '"So is this'
  4. '"This should "also match un"til th"e last "'

In each case it should capture whatever is after the first dquote if there's only spaces before it up to a last quote (which is completely optional). 在每种情况下,如果在第一个双引号之前只有空格,直到最后一个引号(完全可选),它应该捕获什么。

Another thing, a string containing just " shouldn't match ex '""""""""""""' shouldn't match at all. 另一件事,仅包含"的字符串不应该与ex '""""""""""""'完全不匹配。

Also leading and trailing whitespaces are ignored both between the quotes and outside the quotes. 此外,引号之间和引号外的前导空格和尾随空格都会被忽略。

Here's the regex I have so far: 这是我到目前为止的正则表达式:

/^\s*\"\s*(.*?)\s*(?:\"\s*)?$

But it also gets '""""""""""""""""' so that's where I'm stuck. 但是它也会得到'""""""""""""""""'所以这就是我遇到'""""""""""""""""'

What can I do to not match a whole string of just dquotes? 我怎么办才能不匹配整个dquotes字符串?

EDIT: 编辑:

I think I explained what I want wrong, I want it to match a string of dquotes but not capture any of it 我想我解释了我想要的错误,我希望它与一串dquotes匹配,但不捕获任何一个

You can alter your regex so that the content contains at least one non-" character: 您可以更改您的正则表达式,以使内容至少包含一个非字符:

/^\s*\"\s*(.*?[^"].*?)\s*(?:\"\s*)?$/

Update : If you don't want starting and trailing " to be captured, just allow several of them at the borders: 更新 :如果您不希望捕获“开始和结尾”,则只需在边界处允许其中的几个即可:

/^\s*\"+\s*(.*?)\s*(?:\"+\s*)?$/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM