【问题标题】:Check for a particular substring pattern within a string and get all of its matches检查字符串中的特定子字符串模式并获取其所有匹配项
【发布时间】:2020-10-21 02:11:55
【问题描述】:

我正在尝试在字符串中搜索并尝试从字符串中提取特定内容。 这是我要解决的示例。

const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870'
i am trying to extract only '+91888123455, +918885558565 +916885123456, +911234569870'

但数字是动态的,它会根据响应而变化

【问题讨论】:

  • 你怎么知道你想提取什么?
  • @Rastalamm 我只想从字符串中提取数字,这些数字将是动态的
  • @luciferluci ...以及可选的逗号/空格怎么样?从 Q. 的呈现方式来看,这些东西应该完全按照原始输入的形式保留。
  • @PeterSeliger 我期待用户的这些类型的输入可能是他们输入逗号/,也许这不是我以这种方式添加的原因。
  • @luciferluci ... "... 可能他们放了逗号/,也可能没有..." ...但这是否意味着需要保留此元数据信息,还是压制它并只使用纯数字会不会更好?

标签: javascript regex string match


【解决方案1】:

关于使用的正则表达式的模式.../\+\d+,{0,1}/g...

  1. \+ ... 匹配一个强制 + 符号 ... 后跟 ...
  2. \d+ ...至少一个数字(或数字序列)...后跟...
  3. ,{0,1} ... 单个但可选的逗号。
  4. g ... 全局搜索/匹配模式。

function extractValidNumberSequences(str) {
  return str.match(/\+\d+,{0,1}/g).join(' ');
}

const test = `
  xtyzjdjgdjf +91888123455, +918885558565 +916885123456,,, +91123456987 dsjk jjd
  sag sadgsadj 43865984 dsjghj, +918885558565 +916885123456,,, +91123456987 dsjk
`;

console.log(`extractValidNumberSequences(test) : "${ extractValidNumberSequences(test) }"`);
.as-console-wrapper { min-height: 100%!important; top: 0; }

【讨论】:

    【解决方案2】:

    最简单的方法是使用正则表达式:/\+\d*/g。 此正则表达式正在搜索所有以 + 开头并在其后有 0 个或多个数字的字符串。

    const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870';
    
    const result = [...string.matchAll(/\+\d*/g)] // Get all the regex matches
      .map(([text]) => text) // Grabbing the first element in the array which is the text
      .join(','); // Join the text together
    
    console.log(result);

    资源

    【讨论】:

    • 即使子字符串不存在,这也会返回匹配项。
    • 或者只是:js const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870'; const result = string.match(/\+\d*/g).join(','); console.log(result);
    • @baradm100 ...这种方法/正则表达式不能满足OP保留原始字符串提供的逗号/空格的需求(至少看起来像Q.)... OP 想要'+91888123455, +918885558565 +916885123456, +911234569870'
    【解决方案3】:

    var stringVal = "'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870".replace('xtyzjdjgdjf','');

    console.log(stringVal); //打印:+91888123455, +918885558565 +916885123456, +911234569870

    对于所有要丢弃的事件,请使用: 例如—— var ret = "data-123".replace(/data-/g,''); PS:replace函数返回一个新字符串,原字符串不变,所以使用replace()调用后的函数返回值。

    【讨论】:

      【解决方案4】:

      好的,让我直截了当地说:您要从以“+”符号开头并以“,”字符结尾的字符串中提取数字序列?

      您可以这样做的方法是遍历字符串。

      let Raw = "udhaiuedh +34242, +132354"
      
      function ExtractNumbers(ToDecode) {
          let StartSet = ["+"]
          let EndSet = [","," "]
          let Cache = ""
          let Finished = []
          
          for (Char of ToDecode) {
              if (StartSet.includes(Char)) {
                  if (Cache.length != 0) {
                      Finished.push(Cache)
                  }
                  Cache = ""
              } else if (EndSet.includes(Char)) {
                  if (Cache.length != 0) {
                      Finished.push(Cache)
                  }
                  Cache = ""
              } else {
                  if (Number(Char)) {
                      Cache = Cache + String(Char)
                  }
              }
          }
          if (Cache.length != 0) {
              Finished.push(Cache)
          }
          return Finished
      }
      
      console.log(ExtractNumbers(Raw))
      

      这并不完美,但可以帮助您入门:)

      【讨论】:

        【解决方案5】:
        const string = 'xtyzjdjgdjf +91888123455, +918885558565 +916885123456, +911234569870'
        const extractMe = '+91888123455, +918885558565 +916885123456, +911234569870'
            
        if (string.includes(extractMe)) {
           // extractMe is what you want to extract
        }
        

        【讨论】:

        • 抱歉,字符串中的这些数字是动态的
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-30
        • 2015-06-17
        • 1970-01-01
        • 2021-11-29
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        相关资源
        最近更新 更多