用数字和字符分隔字符串

时间:2019-06-10 12:11:02

标签: javascript

我正在尝试格式化字符串以产生正确格式的新字符串:

我有以下字符串(左),应设置格式以匹配(右):

  [ 'xx9999', 'XX-99-99' ],
  [ '9999xx', '99-99-XX' ],
  [ '99xx99', '99-XX-99' ],
  [ 'xx99xx', 'XX-99-XX' ],
  [ 'xxxx99', 'XX-XX-99' ],
  [ '99xxxx', '99-XX-XX' ],
  [ '99xxx9', '99-XXX-9' ],
  [ '9xxx99', '9-XXX-99' ],
  [ 'xx999x', 'XX-999-X' ],
  [ 'x999xx', 'X-999-XX' ],
  [ 'xxx99x', 'XXX-99-X' ],
  [ 'x99xxx', 'X-99-XXX' ],
  [ '9xx999', '9-XX-999' ],
  [ '999xx9', '999-XX-9' ]

我尝试了以下操作,但无法使其正常工作:

const formatLp = (userInput) => {
  if (userInput) {
     return userInput.toUpperCase().match(/[a-z]+|[^a-z]+/gi).join('-');
  }
}

这对其中某些人有效,例如99xxx9,但对其他人无效,例如xx9999

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

使用两次.replace-一次在4个重复的数字/非数字之间插入一个-,一次在数字和字母字符之间插入一个-

const arr = [
  [ 'xx9999', 'XX-99-99' ],
  [ '9999xx', '99-99-XX' ],
  [ '99xx99', '99-XX-99' ],
  [ 'xx99xx', 'XX-99-XX' ],
  [ 'xxxx99', 'XX-XX-99' ],
  [ '99xxxx', '99-XX-XX' ],
  [ '99xxx9', '99-XXX-9' ],
  [ '9xxx99', '9-XXX-99' ],
  [ 'xx999x', 'XX-999-X' ],
  [ 'x999xx', 'X-999-XX' ],
  [ 'xxx99x', 'XXX-99-X' ],
  [ 'x99xxx', 'X-99-XXX' ],
  [ '9xx999', '9-XX-999' ],
  [ '999xx9', '999-XX-9' ]
];
arr.forEach(([str]) => {
  const result = str.toUpperCase()
    .replace(/\d{4}|\D{4}/, substr => `${substr.slice(0, 2)}-${substr.slice(2)}`)
    .replace(/[a-z]{4}|\d(?=[a-z])|[a-z](?=\d)/gi, '$&-');
  console.log(result);
});

您也可以先匹配再加入-匹配3个非数字或3个数字或1-2个非数字或1-2个数字:

const arr = [
  [ 'xx9999', 'XX-99-99' ],
  [ '9999xx', '99-99-XX' ],
  [ '99xx99', '99-XX-99' ],
  [ 'xx99xx', 'XX-99-XX' ],
  [ 'xxxx99', 'XX-XX-99' ],
  [ '99xxxx', '99-XX-XX' ],
  [ '99xxx9', '99-XXX-9' ],
  [ '9xxx99', '9-XXX-99' ],
  [ 'xx999x', 'XX-999-X' ],
  [ 'x999xx', 'X-999-XX' ],
  [ 'xxx99x', 'XXX-99-X' ],
  [ 'x99xxx', 'X-99-XXX' ],
  [ '9xx999', '9-XX-999' ],
  [ '999xx9', '999-XX-9' ]
];
arr.forEach(([str]) => {
  const result = str.toUpperCase()
    .match(/[a-z]{3}|\d{3}|[a-z]{1,2}|\d{1,2}/gi)
    .join('-');
  console.log(result);
});

答案 1 :(得分:0)

我刚刚使用Stack实现了此功能。这是功能。您只需要将字符串传递给此函数即可。

const convert = (str) => {
    let stack = str.split('').reduce((newArr, char, index) => {
        if(index !== 0 && newArr[newArr.length-1] !== char) {
            newArr.push('-');
            newArr.push(char);
            return newArr;
        } else {
              newArr.push(char);
              return newArr;
        }

    },[])
    return stack.join('').toUpperCase();
}

// Here you can check this in action
const convert = (str) => {
    let stack = str.split('').reduce((newArr, char, index) => {
        if(index !== 0 && newArr[newArr.length-1] !== char) {
            newArr.push('-');
			newArr.push(char);
			return newArr;
        } else {
			newArr.push(char);
        	return newArr;
		}
		
    },[])
	return stack.join('').toUpperCase();
}

const strings = [ 'xx9999','9999xx','99xx99','xx99xx','xxxx99','99xxxx','99xxx9','9xxx99','xx999x','x999xx','xxx99x','x99xxx','9xx999','999xx9',];


strings.map(string => console.log(convert(string)))