【问题标题】:splitting a string with a decimal number and some characters [duplicate]用十进制数和一些字符分割字符串[重复]
【发布时间】:2018-09-27 23:30:56
【问题描述】:

我有这样的字符串: '1234picas' '1234 像素' '145.4us'

我想将它们分成两部分:数字部分和非数字部分。例如:'1234.4us'需要拆分为'1234.4'和'us'。我很想在最后一个数字和非数字之间插入一个分隔符并使用拆分,但是在 JavaScript 中是否有更好的方法来做到这一点

谢谢

注意:这不是在特殊字符处分割字符串。它可以转换为,但这是我试图避免的。

【问题讨论】:

  • 使用正则表达式
  • 我正在阅读 Scala 的 RegExp,我变得更加困惑......我会再调查一下
  • google.com/… 获得近 100 万条其他结果
  • 这不是在特殊字符处拆分字符串。这可能是一条可以遵循的道路,但我试图避免它。

标签: javascript reactjs jsx


【解决方案1】:

您可以使用String.prototype.match()

const a = '1234picas';
const b = '1234 px';
const c = '145.4us';

function split(input) {
  const splitArray = input.match(/([\d\.]+)(.*)/); // match only digits and decimal points in the first group and match the rest of the string in second group
  
  return {
    numeric: splitArray[1],
    nonnumeric: splitArray[2],
  };
}

console.log(split(a));
console.log(split(b));
console.log(split(c));

Regex explanation | Regex101

【讨论】:

    【解决方案2】:

    正则表达式!
    以下是它的工作原理:https://regex101.com/r/XbI7Mq/1

    const test = ['1234picas', '1234 px', '145.4us', 'no'];
    const regex = /^(\d+\.?\d+)\s?(.*)/;
    test.forEach(i => {
      const result = regex.exec(i);
      if (result) {
        console.log(result[1], result[2])
      }
    });

    【讨论】:

      【解决方案3】:

      这可能会对您有所帮助:

      var a = "4343.453fsdakfjdsa";
      a.match(/[a-zA-Z]+|[\d\.?]+/ig);
      

      MDN for String.match

      它基本上说匹配字母或匹配带有可选句点的数字。 ig 是不敏感的(这并不是真正需要的)和全局的,因为在第一次匹配时不返回,一直到所有字符串都被解析为止。

      【讨论】:

        【解决方案4】:

        这是使用 parseFloat()slice() 的一种方法,如果需要,可以将其添加到 Sting.prototype

        const str1 = '1234picas',
          str2 = '1234 px',
          str3 = '145.4us';
        
        String.prototype.split = function () {
          const num = parseFloat(this);
          const alph = this.slice(num.toString().length, this.length)
        
          return {
            num,
            alph
          }
        }
        
        
        console.log(str1.split());
        console.log(str2.split());
        console.log(str3.split());

        【讨论】:

        • 所有这些变量都可以是const 而不是let :)
        • @RahulDesai 是的,你是对的 :)
        【解决方案5】:

        您可以在 javascript 中这样做来拆分:

        var myString = '145.4us';
        var splits = myString.split(/(\d+\.?\d+)/);
        console.log(splits);
        

        【讨论】:

          【解决方案6】:

          您可以将.split 与使用前瞻的正则表达式一起使用:

          str.split(/(?=[^\d.-])/g))
             .map(y => [
                   y[0],
                   y.slice(1).join('').trim()
             ])
          

          x = ["1234picas", "1234 px", "145.4us"];
          
          console.log(x.map(y => 
              y.split(/(?=[^\d.-])/g))
               .map(y => [
                   y[0],
                   y.slice(1).join('').trim()
               ])
          )

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-03-30
            • 2011-08-19
            • 1970-01-01
            • 2015-03-09
            • 2019-03-19
            • 2014-02-26
            • 1970-01-01
            相关资源
            最近更新 更多