简体   繁体   English

RegEx:匹配用单引号括起来的字符串,但不匹配双引号内的字符串

[英]RegEx : Match a string enclosed in single quotes but don't match those inside double quotes

I wanted to write a regex to match the strings enclosed in single quotes but should not match a string with single quote that is enclosed in a double quote. 我想编写一个正则表达式来匹配用单引号括起来的字符串,但是不应该匹配带有双引号的单引号的字符串。

Example 1: 例1:

a = 'This is a single-quoted string';

the whole value of a should match because it is enclosed with single quotes. 整体价值应该匹配,因为它是封闭的单引号。

EDIT: Exact match should be: 'This is a single-quoted string' 编辑:完全匹配应该是: '这是一个单引号字符串'

Example 2: 例2:

x = "This is a 'String' with single quote";

x should not return any match because the single quotes are found inside double quotes. x不应返回任何匹配项,因为单引号位于双引号内。

I have tried /'.*'/g but it also matches the single quoted string inside a double quoted string. 我试过/'.*'/g但它也匹配双引号字符串中的单引号字符串。

Thanks for the help! 谢谢您的帮助!

EDIT: 编辑:

To make it clearer 使它更清楚

Given the below strings: 鉴于以下字符串:

The "quick 'brown' fox" jumps
over 'the lazy dog' near
"the 'riverbank'".

The match should only be: 匹配应该只是:

'the lazy dog'

Assuming that won't have to deal with escaped quotes (which would be possible but make the regex complicated), and that all quotes are correctly balanced (nothing like It's... "Monty Python's Flying Circus"! ), then you could look for single-quoted strings that are followed by an even number of double quotes: 假设不必处理转义引用(这可能会使正则表达式复杂化),并且所有引用都是正确平衡的(没有像It's... "Monty Python's Flying Circus"! ),那么你可以看一下对于单引号字符串,后跟偶数个双引号:

/'[^'"]*'(?=(?:[^"]*"[^"]*")*[^"]*$)/g

See it live on regex101.com . 在regex101.com上直播

Explanation: 说明:

'        # Match a '
[^'"]*   # Match any number of characters except ' or "
'        # Match a '
(?=      # Assert that the following regex could match here:
 (?:     # Start of non-capturing group:
  [^"]*" # Any number of non-double quotes, then a quote.
  [^"]*" # The same thing again, ensuring an even number of quotes.
 )*      # Match this group any number of times, including zero.
 [^"]*   # Then match any number of characters except "
 $       # until the end of the string.
)        # (End of lookahead assertion)

Try something like this: 尝试这样的事情:

^[^"]*?('[^"]+?')[^"]*$

Live Demo 现场演示

If you are not strictly bounded by the regex, you could use the function "indexOf" to find out if it's a substring of the double quoted match: 如果你没有严格限制正则表达式,你可以使用函数“indexOf”来找出它是否是双引号匹配的子字符串:

var a = "'This is a single-quoted string'";
var x = "\"This is a 'String' with single quote\"";

singlequoteonly(x);

function singlequoteonly(line){
    var single, double = "";
    if ( line.match(/\'(.+)\'/) != null ){
        single = line.match(/\'(.+)\'/)[1];
    }
    if( line.match(/\"(.+)\"/) != null ){
        double = line.match(/\"(.+)\"/)[1];
    }

    if( double.indexOf(single) == -1 ){
        alert(single + " is safe");
    }else{
        alert("Warning: Match [ " + single + " ] is in Line: [ " + double + " ]");
    }
}

See the JSFiddle below: 请参阅下面的JSFiddle:

JSFiddle 的jsfiddle

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

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