【问题标题】:Looking for an efficient way to return a portion of a string寻找一种有效的方法来返回字符串的一部分
【发布时间】:2021-07-06 11:15:32
【问题描述】:

我正在使用 JavaScript/TypeScript。

我有一个字符串,其中可能包含一些表示重要性或紧迫性的标签。示例:

A:“提醒我明天去店里,真的很重要”

B:“明天去商店很重要。”

我已经建立了一种方法来识别重要短语在主要短语中的位置。例如,对于字符串 A,重要性短语位于字符 40 和 62 之间。在短语 B 中,它位于字符 0 和 16 之间。

我有一个 JSON 对象来描述重要性短语的开始/结束点:

this.importancePosition = { start: 40, stop: 62 }

没问题。

所以我还有其他短语元素检测器:

this.urgencyPosition = { start: 20, stop: 30 }

this.locationPosition = { start: -1, stop: -1 }

等等。

顺便说一句,我使用 -1 作为未找到令牌的指示符。此外,子短语可能不按顺序排列。

那么问题来了:

假设我有主要短语和每个检测到的子短语的位置。现在,我想返回主短语中不属于子短语之一的所有内容。

我有这个:

[aaaaaaaaaaaaaaaaa[subphrase]bbbb[subphrase]ccccccccccccccccccccccccccccccccccccccccc[subphrase]]

我想退货:

[aaaaaaaaaaaaaaaaa bbbb ccccccccccccccccccccccccccccccccccccccccc]

什么是执行此操作的有效方法?我考虑过将它们从主字符串中正则表达式,但这是最干净的方式吗?

【问题讨论】:

  • 如果您有索引,您可以只使用索引来重建字符串(如果相同的子字符串在原始字符串中多次出现,则正则表达式也容易出错)。您可能会从查找专注于“查找给定时间表的空闲时间范围”类型问题的算法中受益最多。而不是“时间”,它只是“指数”,但它的工作原理是一样的。例如:stackoverflow.com/questions/48407713/…
  • 在我的例子中,我最终使用了正则表达式方法,因为我知道从原文中提取的确切措辞,因此正则表达式的可靠性会很高。但是通过阅读您提供的链接,我学到了一些东西。谢谢你。

标签: javascript regex string typescript


【解决方案1】:

假设 mainPhrase 是一个字符串,试试:

var replacedPhrase = mainPhrase.replace(/\[subphrase\]/g, " ");

我不确定我对 JS 中的正则表达式有多生疏,但我认为这应该可行。

【讨论】:

    【解决方案2】:

    function getSpliceList(dataType, positionKeyList) {
      return positionKeyList.reduce((list, positionKey) => {
      
        const { start, stop } = dataType[positionKey]
        list.push([start, stop]);
    
        return list;
    
      }, []).sort((a, b) => a[0] - b[0]);
    }
    function getReassambledStringBySpliceConfig(str, dataType, positionKeyList) {
      let delimiter = '';
    
      return getSpliceList(dataType, positionKeyList)
        .reduceRight((characterList, spliceItem) => {
    
          characterList.splice(
            spliceItem[0],
            spliceItem[1] - spliceItem[0] + 1,
            delimiter
          );
          if (!delimiter) {
            delimiter = ' ';
          }
          return characterList;
    
        }, str.split('')).join('');
    }
    
    const sampleItem = {
      foo: "foo",
      importancePosition: { start: 85, stop: 95 },
      bar: "bar",
      urgencyPosition: { start: 33, stop: 43 },
      baz: "baz",
      locationPosition: { start: 18, stop: 28 },
    };
    const sampleText = '[aaaaaaaaaaaaaaaaa[subphrase]bbbb[subphrase]ccccccccccccccccccccccccccccccccccccccccc[subphrase]]';
    
    const expectedResult = '[aaaaaaaaaaaaaaaaa bbbb ccccccccccccccccccccccccccccccccccccccccc]';
    
    console.log(
      '(ordered) splice list ...',
    
      getSpliceList(
        sampleItem,
        ['urgencyPosition', 'importancePosition', 'locationPosition']
      )
    );
    
    console.log(
      'reassembled string ...',
    
      getReassambledStringBySpliceConfig(
        sampleText,
        sampleItem,
        ['urgencyPosition', 'importancePosition', 'locationPosition']
      )
    );
    console.log(
      'does reassembled string equal expected result ?..',
    
      getReassambledStringBySpliceConfig(
    
        sampleText,
        sampleItem,
        ['urgencyPosition', 'importancePosition', 'locationPosition']
    
      ) === expectedResult
    );
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    【讨论】:

    • @user3093211 ...对上述方法有任何疑问吗?
    • 哇,你做了很多工作。远远超出我的预期。我真的很喜欢你建议的方法的泛化能力。我当前的应用程序不需要同样广泛的应用程序,但我肯定会在未来的解决方案上贴上便利贴。这是一种深思熟虑的方式。
    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2023-04-11
    • 2013-11-08
    • 1970-01-01
    • 2011-08-04
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多