简体   繁体   中英

regular expression for deleting zeros that come immediately after a non-digit

I am writing code to convert numbers counted in thousands (separated in writing in chunks of 3) into myriads (separated in writing in chunks of 4) for Japanese formatting.

the current code produces following results:
ex)
input: 123,456,789,123,456
output: 123兆4567億8912万3456

Using regular expressions I have been able to delete sequences of four 0's and the proceding character with myriad = myriad.replace(/0000\\D/g, "");
result:
input: 12,300,002,345
output: 123億2345 ( 0000万 was deleted)

However, the code currently doesn't delete unnessecary zero's:
ex)
input: 32,131,200,232,132
output: 32兆1312億0023万2132

(I would like to delete the two zeros before 23万 )

I am trying to find a regex solution to this and have attempted with
myriad = myriad.replace(/?=0{1,3}/g, ""); to no avail... I am rather stumped, any suggestions would be helpful

EDIT: I think the regex should replace 0's that follow any \\D , but I can't figure out how to delete them without deleting the preceding character as well

EDIT: working app:

 <!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> 

You need to use capturing group.

string.replace(/(\D)0+/g, "$1")

(\\D) captures a non-digit character and the following 0+ would match one or more 0's. Replacing the matched chars with the chars present inside the group index 1 will give you the desired output.

I find regex fiddles very helpful for this kind of thing. I made one here: https://regex101.com/r/jG3aB5/1

I think the regex that will solve your problem is this one:

(?:[^0-9])*(0*)

It matches an arbitrary number of zeros that follow a single non-digit character. The non-digit character is not captured and will not be replaced.

Another approach: Use lookbehind to not include the match character \\D but match only zeros. Please see the working DEMO

/(?!\D)0+/g

In your case:

myriad = myriad.replace(/(?!\D)0+/g, "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM