我正在编写代码,将数千个数字(以3个块的形式分开)转换为myriads(以4个块的形式分开)以进行日语格式化。
当前代码产生以下结果:
EX)
输入:123,456,789,123,456
输出:123兆4567億8912万3456
使用正则表达式,我能够删除四个0的序列,并使用myriad = myriad.replace(/0000\D/g, "");
删除程序字符。
结果:
输入:12,300,002,345
输出:123億2345
(0000万
已删除)
然而,该代码目前并没有删除不必要的零码:
EX)
输入:32,131,200,232,132
输出:32兆1312億0023万2132
(我想在23万
)
我正在尝试找到一个正则表达式的解决方案并试图用myriad = myriad.replace(/?=0{1,3}/g, "");
无效...我很难过,任何建议都会有所帮助
编辑: 我认为正则表达式应该替换跟随任何\ D的0,但我无法弄清楚如何删除它们而不删除前面的字符
编辑:工作应用:
<!DOCTYPE html>
<html>
<head>
<title>変換天才</title>
<script>
//myriad converter function help from Stack Overflow user paxdiablo
function makeNum(num) {
num = num.replace(/,/g,""); //remove commas
var markers = "万億兆京該秭穣溝澗正載極";
var result = "";
//regroup in myriads
while (num.length > 4) {
if (markers.length == 0) {
result = "(?)" + num.substr(num.length-4) + result;
} else {
result = markers.substr(0, 1) + num.substr(num.length-4) + result;
markers = markers.substr(1);
}
num = num.substr(0, num.length-4);
}
return num + result;
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// insert commas for readability
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function makeCom(num){
num = num.replace(/,/g, "");
var result = num.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
return result;
}
function convert(){
var innum = document.getElementById("input").value;
var parsnum = (innum.replace(/,/g,""));
if (isNaN(parseInt(parsnum)) == true) {
document.getElementById("converted").innerHTML = "Please enter valid number.";
}
else {
var myriad = makeNum(innum);
// delete unnec. zeros
myriad = myriad.replace(/0000\D/g, "");
myriad = myriad.replace(/(\D)0+/g, "$1");
document.getElementById("converted").innerHTML = myriad ;
//display number with commas
var commanum = makeCom(innum);
document.getElementById("commaed").innerHTML = "You entered: " + commanum ;
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// button functions
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
function multiplier(kake) {
var mult = document.getElementById("input").value;
if (mult == "") {
mult = kake;
}
else if (isNaN(mult)==false){
mult = (mult * kake);
}
else {
document.getElementById("converted").innerHTML = "Please enter valid number";
return;
}
document.getElementById("input").value = mult;
}
function thou(){
multiplier(1000);
}
function xMil(){
multiplier(1000000);
}
function xBil(){
multiplier(1000000000);
}
function xTril(){
multiplier(1000000000000);
}
function xQuad(){
multiplier(1000000000000000);
}
function clr(){
document.getElementById("input").value = "";
document.getElementById("converted").innerHTML = "";
}
</script>
</head>
<body>
<div><p>Enter a large whole number (commas OK). </p></div>
<input type="text" id="input"/>
<input type="submit" id="submit" value="Convert" onclick="convert()">
<br>
<input type="button" id="xthou" onclick="thou()" value="thousand">
<input type="button" id="xmil" onclick="xMil()" value="million">
<input type="button" id="xbil" onclick="xBil()" value="billion">
<br>
<input type="button" id="xtril" onclick="xTril()" value="trillion">
<input type="button" id="xquad" onclick="xQuad()" value="quadrillion">
<input type="button" id="clr" onclick="clr()" value="Clr">
<br><br>
<div><span id="commaed"></span></div>
<br>
<div id="converted"></div>
</body>
</html>
&#13;
答案 0 :(得分:3)
您需要使用捕获组。
string.replace(/(\D)0+/g, "$1")
(\D)
捕获非数字字符,以下0+
将匹配一个或多个0。将匹配的字符替换为组索引1中存在的字符将为您提供所需的输出。
答案 1 :(得分:0)
我发现正则表达式小提琴对这种事情非常有帮助。我在这里做了一个: https://regex101.com/r/jG3aB5/1
我认为解决问题的正则表达式是这样的:
(?:[^0-9])*(0*)
它匹配单个非数字字符后面的任意数量的零。不会捕获非数字字符,也不会被替换。
答案 2 :(得分:0)
另一种方法:使用lookbehind不包含匹配字符\D
但仅匹配零。请参阅正常工作DEMO
/(?!\D)0+/g
在你的情况下:
myriad = myriad.replace(/(?!\D)0+/g, "");