JavaScript-基于字典 /对象中的键的分析字符串
在我的React程序中,假设我有以下字典 /对象
const myDict = {
"a":"Value 1",
"b":"Value 2",
"hello":"Value 3",
"Bye":"Value 4"
}
(请注意,没有键在任何其他键中都存在的值 - 例如,没有密钥可以包含a < / code>字母,因为那是另一个密钥)
的排列组成的字符串,例如,可能的字符串条目将是:
- “ abhellob”
- “ ababbyeahello”,
- “ babbyeahello”“ byehellobyeba”
- 等
我将收到一个将仅由此对象的键 我希望创建一个功能,该函数将根据mydict
的键将输入字符串分解为其组成部分。
因此,例如,
“ abhellob”
将变成[“ A”,“ B”,“ Hello”,“ B”]
如何创建这样的函数?我已经尝试过,但是由于不得不处理mydict
的不同长度键而迷失了方向,而且我不知道该怎么做。
In my React program, suppose I had the following dictionary / object
const myDict = {
"a":"Value 1",
"b":"Value 2",
"hello":"Value 3",
"Bye":"Value 4"
}
(note that no keys have values existing in any other keys - For example, no key can contain the a
letter since that is a different key)
And I will be receiving a string that will ONLY CONSIST OF PERMUTATIONS OF THE KEYS TO THIS OBJECT, so, for example, possible string entries will be:
- "abhellob"
- "ababByeahello"
- "ByehelloByeba"
- etc.
And I am looking to create a function that will break the input string down into its constituent parts based upon the keys of myDict
.
So, for example,
"abhellob"
would become["a", "b", "hello", "b"]
How can I create such a function? I've tried, but am getting lost with having to deal with different length keys to myDict
and I don't know how to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在不同键之间交替的正则表达式可以解决问题。
(如果某些键可能具有角色重叠,则开始是对键进行排序,以便最长的键首先。)
A regular expression that alternates between the different keys would do the trick.
(If some keys could have character overlap, a start would be to sort the keys so the longest ones come first.)
您可以在没有正则义务的情况下执行此操作:
You can do this without Regex: