我有一个 14 位长的数字,我需要将其拆分为以下格式:
xxx xxx xxx xxxxx
我有一个正则表达式,从末尾开始每 3 个字符拆分一次(因为前瞻?)
(?=(\d{3})+(?!\d))
这给了我:
xx xxx xxx xxx xxx
我尝试在 regex101.com 中使用lookbehind,但出现模式错误...
(?<=(\d{3})+(?!\d))
我如何使用lookbehind使其从字符串的开头开始(如果这是我的问题)以及如何仅重复该模式3次然后切换到a \d{5}
?
试试这个:
(\d{3})(\d{3})(\d{3})
并替换为:
"$1 $2 $3 "
const regex = /(\d{3})(\d{3})(\d{3})/g;
const str = `12345678901234`;
const subst = `$1 $2 $3 `;
const result = str.replace(regex, subst);
console.log( result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句