简体   繁体   English

正则表达式,用于删除非数字后紧跟的零

[英]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. 我正在编写代码,以将成千上万的数字(以3的大写形式分隔)转换为无数(以4的大写形式分隔)日语格式。

the current code produces following results: 当前代码产生以下结果:
ex) 例如)
input: 123,456,789,123,456 输入: 123,456,789,123,456
output: 123兆4567億8912万3456 输出: 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, ""); 使用正则表达式,我已经能够删除四个0的序列以及前myriad = myriad.replace(/0000\\D/g, "");
result: 结果:
input: 12,300,002,345 输入: 12,300,002,345
output: 123億2345 ( 0000万 was deleted) 输出: 123億2345 (删除了0000万

However, the code currently doesn't delete unnessecary zero's: 但是,该代码当前不会删除不必要的零:
ex) 例如)
input: 32,131,200,232,132 输入: 32,131,200,232,132
output: 32兆1312億0023万2132 输出: 32兆1312億0023万2132

(I would like to delete the two zeros before 23万 ) (我想删除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 编辑:我认为正则表达式应该替换任何\\ D后面的0,但是我不知道如何删除它们而不删除前面的字符

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. (\\D)捕获一个非数字字符,并且后面的0+将匹配一个或多个0。 Replacing the matched chars with the chars present inside the group index 1 will give you the desired output. 用组索引1中存在的字符替换匹配的字符将为您提供所需的输出。

I find regex fiddles very helpful for this kind of thing. 我发现正则表达式小提琴对这种事情很有帮助。 I made one here: https://regex101.com/r/jG3aB5/1 我在这里做了一个: 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. 另一种方法:使用向后搜索不包含匹配字符\\D而仅匹配零。 Please see the working DEMO 请查看正在运行的演示

/(?!\D)0+/g

In your case: 在您的情况下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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