【问题标题】:how to pick middle text between 2 changing string如何在 2 个变化的字符串之间选择中间文本
【发布时间】:2017-04-01 23:07:03
【问题描述】:

如何从

获取中心文本in yeshvanthpur , bangalore,即yeshvanthpur

字符串。

in[constant always] yeshvanthpur[changes every time] , bangalore [changes every time]

在上述模式中,in 每次都会保持不变。其中yeshvanthpurbangalore 每次都会改变。

问题:我想得到yeshvanthpur

测试用例:

in yehlanka , bangalore  // expected output : yehlanka 

in bandra , mumbai     // expected output : bandra 

in mathikere , bangalore   // expected output : mathikere 

请多多包涵,我不知道regex

我唯一的代码

var str = 'in yehlanka , bangalore';

请帮助我提前谢谢

【问题讨论】:

  • 试试这个:var res = str.split(' ')[1]
  • 明确您的需求。这让我很困惑。

标签: javascript jquery node.js


【解决方案1】:

这取决于字符串在不同空间中的趋势。

  1. 不能使用逗号 (',') 进行拆分,因为它可能出现在字符串的不同部分。
  2. 您不能使用“yeshvanthpur”或“in”或任何其他词进行拆分,因为它们可能会再次出现在不同的部分。

我建议使用NLP (Natural Language Processing),但您需要对其进行研究才能获得准确的结果。

这是在 node.js 中使用 NLP 的 module

【讨论】:

    【解决方案2】:

    如果在 and 中,试试这个,会保持不变

    var str = 'in yehlanka , bangalore';
    
    
    str.split(',')[0].split('in')[1].trim() //output yehlanka
    

    【讨论】:

      【解决方案3】:

      试试这个:

      var str = 'in yehlanka , bangalore';
      
      var word = str.substring(3,str.indexOf(",")-1); //start index is 3 because 'in ' is a 3 char string
      // last index is first occurrence of ',' 
      
      alert(word);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-27
        • 2023-01-04
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 2013-12-31
        • 2021-10-30
        • 1970-01-01
        相关资源
        最近更新 更多