gpt4 book ai didi

javascript - 匹配所有内容但不匹配带引号的字符串

转载 作者:行者123 更新时间:2023-11-30 09:02:09 26 4
gpt4 key购买 nike

我想匹配所有内容,但不匹配带引号的字符串。

我可以用这个匹配所有引用的字符串:/(("([^"\\]|\\.)*")|('([^'\\]|\\.)* '))/所以我尝试用这个匹配所有但没有引号的字符串: /[^(("([^"\\]|\\.)*")|('([^'\\]|\\.)*'))]/ 但它不起作用。

我只想使用正则表达式,因为我想替换它并想在它返回后得到引用的文本。

string.replace(regex, function(a, b, c) {
// return after a lot of operations
});

带引号的字符串对我来说是这样的"bad string" 或这个'cool string'

所以如果我输入:

he\'re is "watever o\"k" efre 'dder\'4rdr'?

它应该输出这样的匹配:

["he\'re is ", " efre ", "?"]

而且我不想替换它们。

我知道我的问题很难,但并非不可能!没有什么是不可能的。

谢谢

最佳答案

编辑:重写以涵盖更多边缘情况。

这个可以做,但是有点复杂。

result = subject.match(/(?:(?=(?:(?:\\.|"(?:\\.|[^"\\])*"|[^\\'"])*'(?:\\.|"(?:\\.|[^"'\\])*"|[^\\'])*')*(?:\\.|"(?:\\.|[^"\\])*"|[^\\'])*$)(?=(?:(?:\\.|'(?:\\.|[^'\\])*'|[^\\'"])*"(?:\\.|'(?:\\.|[^'"\\])*'|[^\\"])*")*(?:\\.|'(?:\\.|[^'\\])*'|[^\\"])*$)(?:\\.|[^\\'"]))+/g);

会回来

, he said. 
, she replied.
, he reminded her.
,

从此字符串中(为清楚起见,添加了换行符并删除了引号):

"Hello", he said. "What's up, \"doc\"?", she replied. 
'I need a 12" crash cymbal', he reminded her.
"2\" by 4 inches", 'Back\"\'slashes \\ are OK!'

解释:(有点令人难以置信)

分解正则表达式:

(?:
(?= # Assert even number of (relevant) single quotes, looking ahead:
(?:
(?:\\.|"(?:\\.|[^"\\])*"|[^\\'"])*
'
(?:\\.|"(?:\\.|[^"'\\])*"|[^\\'])*
'
)*
(?:\\.|"(?:\\.|[^"\\])*"|[^\\'])*
$
)
(?= # Assert even number of (relevant) double quotes, looking ahead:
(?:
(?:\\.|'(?:\\.|[^'\\])*'|[^\\'"])*
"
(?:\\.|'(?:\\.|[^'"\\])*'|[^\\"])*
"
)*
(?:\\.|'(?:\\.|[^'\\])*'|[^\\"])*
$
)
(?:\\.|[^\\'"]) # Match text between quoted sections
)+

首先,你可以看到有两个相似的部分。这两个先行断言确保前面的字符串中有偶数个单引号/双引号,忽略转义引号和相反类型的引号。我将用单引号部分显示它:

(?=                   # Assert that the following can be matched:
(?: # Match this group:
(?: # Match either:
\\. # an escaped character
| # or
"(?:\\.|[^"\\])*" # a double-quoted string
| # or
[^\\'"] # any character except backslashes or quotes
)* # any number of times.
' # Then match a single quote
(?:\\.|"(?:\\.|[^"'\\])*"|[^\\'])*' # Repeat once to ensure even number,
# (but don't allow single quotes within nested double-quoted strings)
)* # Repeat any number of times including zero
(?:\\.|"(?:\\.|[^"\\])*"|[^\\'])* # Then match the same until...
$ # ... end of string.
) # End of lookahead assertion.

双引号部分作用相同。

然后,在字符串中这两个断言成功的每个位置,正则表达式的下一部分实际上会尝试匹配某些内容:

(?:      # Match either
\\. # an escaped character
| # or
[^\\'"] # any character except backslash, single or double quote
) # End of non-capturing group

整个过程重复一次或多次,尽可能多次。 /g 修饰符确保我们获得字符串中的所有匹配项。

See it in action here on RegExr .

关于javascript - 匹配所有内容但不匹配带引号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8375618/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com