gpt4 book ai didi

javascript - 在这种情况下最好的 Javascript 字符串搜索和返回技术?

转载 作者:行者123 更新时间:2023-11-30 17:41:49 25 4
gpt4 key购买 nike

我有一个字符串进入一个函数,它可能是:

I am a "somevalue"

I am a "somevalue" of "anothervalue"

在每种情况下,我都需要识别“我是一个”部分,然后返回引号内的值,或者如果有两个则返回两者。有几种方法可以做到这一点,但我正在寻找高使用率时最有效的方法。

有兴趣听取任何对此有意见的人的意见 - 谢谢!

最佳答案

由于您的格式是不变的,您可以在同一个正则表达式中进行匹配和捕获。

var str1 = 'I am a "somevalue" of "anothervalue"',
str2 = 'I am a "somevalue"',
str3 = 'I am a "value with \\"escaped\\" quotes"',
regex = /^I am a "((?:\\"|[^"])*)"(?: of "((?:\\"|[^"])*)")?/;

function match(str) {
var matches = str.match(regex);
if (matches !== null) {
console.log(matches.slice(1)); // ["somevalue", "anothervalue"]
}
}

match(str1); // ["somevalue", "anothervalue"]
match(str2); // ["somevalue", undefined]
match(str3); // ["value with \"escaped\" quotes", undefined]

切片调用是删除包含整个字符串的第一个匹配项。如果没有匹配项,您将获得“未定义”作为第二个匹配项。

根据引号中引号的转义方式,您可能需要稍微修改一下正则表达式。我假设 \ 将成为转义字符。

NODE                     EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
I am a " 'I am a "'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\\ '\'
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[^"] any character except: '"'
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
of " ' of "'
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
\\ '\'
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[^"] any character except: '"'
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
)? end of grouping

综上所述,此解决方案仅适用于“高使用率”的特定值。如果我们以非常高的速度讨论数百万个查询,那么您最好使用更适合解析文本的技术(这可能不会出现在 JavaScript/节点中)。

关于javascript - 在这种情况下最好的 Javascript 字符串搜索和返回技术?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20921904/

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