javascript字符串提取数字?

需求:要把一串字符拆分成有用的信息,字符串的格式有两种情形
1、abc10
2、123abc100
字符与数字长度不固定,提取后的结果应为:
1、['abc','10']
2、['123','abc','100']

补充需求

输出结果
1、['abc','10']
2、['abc','123','100']
这样使用的时候更方便。

当abc为某个特定的字符时,字符前面的数字与本身合并一起。如:1ab23=>['1ab','23']

阅读 8.4k
5 个回答
/**
 * @param {string} str
 * @param {string|string[]} [keywords]
 * @returns {string}
 */
function match (str, keywords) {
  str = String(str)
  var ks = ''
  if (Array.isArray(keywords)) {
    ks = keywords.map(k => '\\d+' + k).join('|') + '|'
  } else if (typeof keywords === 'string') {
    ks = `\\d+${keywords}|`
  }
  var checker = new RegExp(`${ks}\\d+|\\D+`, 'g')
  return str.match(checker).sort((a, b) => !isNaN(a * 1) && isNaN(b * 1) ? 1 : -1)
}

console.log(match('123abc100')) // [ 'abc', '123', '100' ]
console.log(match('1ab23', 'ab')) // [ '1ab', '23' ]
console.log(match('ab23', 'ab')) // [ 'ab', '23' ]
console.log(match('1ab23cd244gh3', ['ab', 'cd'])) // [ '1ab', '23cd', 'gh', '244', '3' ]

对应题主的补充答案:

假设特殊字符为abc

function readIntoArray(rawStr) {
  const segments = [];
  temp = '';
  prevType = 0;
  rawStr.split('').forEach((s) => {
    let currentType = (s >= '0' && s <= '9') ? 1 : 2
    if (currentType != prevType && temp != '') {
      if (segments.length > 0 && temp == 'abc' && /\d+/.test(segments[segments.length - 1])) {
        segments[segments.length - 1] += temp;
      } else {
        segments.push(temp);
      }
      temp = s;
    } else {
      temp += s;
    }
    prevType = currentType;
  });
  if (segments.length > 0 && temp == 'abc' && /\d+/.test(segments[segments.length - 1])) {
    segments[segments.length - 1] += temp;
  } else {
    segments.push(temp);
  }

  return segments;
}

console.log(readIntoArray('123abc100'));
console.log(readIntoArray('1abc10'));
console.log(readIntoArray('1abc101abc'));

输出

["123abc", "100"]
["1abc", "10"]
["1abc", "101abc"]

在线把玩: https://jsfiddle.net/1xw93w5L/1/


function readIntoArray(rawStr) {
  const segments = [];
  temp = '';
  prevType = 0;
  rawStr.split('').forEach((s) => {
    let currentType = (s >= '0' && s <= '9') ? 1 : 2
    if (currentType != prevType && temp != '') {
      if (segments.length > 1 && temp == 'abc' && /\d+/.test(segments[segments.length-1])) {
          segments[segments.length-1] += temp;
      } else {
          segments.push(temp);
      }
      temp = s;
    } else {
      temp += s;
    }
    prevType = currentType;
  });
  segments.push(temp);

  return segments;
}

console.log(readIntoArray('123abc100'));
console.log(readIntoArray('abc10'));

输出:

["123", "abc", "100"]
["abc", "10"]

在线把玩:https://jsfiddle.net/1xw93w5L/

"446abc1234".match(/[\d]+|[\D]+/g)

试了下应该是可以的,首先把数字替换成^^包围的形式,^^可以是任何不可能出现在你的字符串里面的特殊字符:

'123abc100'.replace(/(\d+)/g ,'^^$1')
// "^^123abc^^100"

然后以^^分割:

"^^123abc^^100".split('^^')
// ["", "123abc", "100"]

最后过滤掉空字符串:

["", "123abc", "100"].filter(t => !!t)
// ["123abc", "100"]

写在一起就是:

'123abc100'.replace(/(\d+)/g ,'^^$1').split('^^').filter(a => !!a)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏