JavaScript-基于字典 /对象中的键的分析字符串

发布于 2025-01-22 09:33:41 字数 665 浏览 0 评论 0原文

在我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2025-01-29 09:33:41

在不同键之间交替的正则表达式可以解决问题。

const myDict = {
    "a":"Value 1",
    "b":"Value 2",
    "hello":"Value 3",
    "Bye":"Value 4"
}
const pattern = new RegExp(
  Object.keys(myDict).join('|'),
  'g'
);
console.log("abhellob".match(pattern));
console.log("ababByeahello".match(pattern));
console.log("ByehelloByeba".match(pattern));

(如果某些键可能具有角色重叠,则开始是对键进行排序,以便最长的键首先。)

const myDict = {
    "a":"Value 1",
    "b":"Value 2",
    "hello":"Value 3",
    "Bye":"Value 4"
}
const pattern = new RegExp(
  Object.keys(myDict)
    .sort((a, b) => a.length - b.length)
    .join('|'),
  'g'
);
console.log("abhellob".match(pattern));
console.log("ababByeahello".match(pattern));
console.log("ByehelloByeba".match(pattern));

A regular expression that alternates between the different keys would do the trick.

const myDict = {
    "a":"Value 1",
    "b":"Value 2",
    "hello":"Value 3",
    "Bye":"Value 4"
}
const pattern = new RegExp(
  Object.keys(myDict).join('|'),
  'g'
);
console.log("abhellob".match(pattern));
console.log("ababByeahello".match(pattern));
console.log("ByehelloByeba".match(pattern));

(If some keys could have character overlap, a start would be to sort the keys so the longest ones come first.)

const myDict = {
    "a":"Value 1",
    "b":"Value 2",
    "hello":"Value 3",
    "Bye":"Value 4"
}
const pattern = new RegExp(
  Object.keys(myDict)
    .sort((a, b) => a.length - b.length)
    .join('|'),
  'g'
);
console.log("abhellob".match(pattern));
console.log("ababByeahello".match(pattern));
console.log("ByehelloByeba".match(pattern));

懒的傷心 2025-01-29 09:33:41

您可以在没有正则义务的情况下执行此操作:

function getFragments(entryString){
  const myDict = {
"a":"Value 1",
"b":"Value 2",
"hello":"Value 3",
"Bye":"Value 4"
  }
  const keys = Object.keys(myDict);
  
  const result = [];
  let remainingString = entryString;
  while (remainingString) {
const nextWord = keys.find(key => remainingString.startsWith(key));
if (!nextWord) throw new Error("Couldn't match with word");

result.push(nextWord);
remainingString = remainingString.slice(nextWord.length);
  }
  
  return result;
}

console.log(getFragments("abhellob"));
console.log(getFragments("ababByeahello"));
console.log(getFragments("ByehelloByeba"));

You can do this without Regex:

function getFragments(entryString){
  const myDict = {
"a":"Value 1",
"b":"Value 2",
"hello":"Value 3",
"Bye":"Value 4"
  }
  const keys = Object.keys(myDict);
  
  const result = [];
  let remainingString = entryString;
  while (remainingString) {
const nextWord = keys.find(key => remainingString.startsWith(key));
if (!nextWord) throw new Error("Couldn't match with word");

result.push(nextWord);
remainingString = remainingString.slice(nextWord.length);
  }
  
  return result;
}

console.log(getFragments("abhellob"));
console.log(getFragments("ababByeahello"));
console.log(getFragments("ByehelloByeba"));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文