简体   繁体   English

如何在包含前导零的JavaScript中增加字符串?

[英]How to increment a string in JavaScript containing leading zeros?

I have string like: 我有这样的字符串:

MPG_0023 MPG_0023

I want to find something like 我想找到类似的东西

MPG_0023 + 1 MPG_0023 + 1

and I should get 我应该得到

MPG_0024 MPG_0024

How to do that in JavaScript? 如何在JavaScript中做到这一点? It should take care that if there are no leading zeros, or one leading zero should still work like MPG23 should give MPG24 or MPG023 should give MPG024. 请注意,如果没有前导零,或者一个前导零仍然可以正常工作,如MPG23应该给MPG24或MPG023应该给MPG024。

There should be no assumption that there is underscore or leading zeros, the only thing is that first part be any string or even no string and the number part may or may not have leading zeros and it is any kind of number so it should work for 0023 ( return 0024) or for gp031 ( return gp032) etc. 不应假设存在下划线或前导零,唯一的事情是,第一部分可以是任何字符串,甚至没有字符串,并且数字部分可以具有也可以不具有前导零,并且它是任何类型的数字,因此它应适用于0023(返回0024)或gp031(返回gp032)等。

Here's a quick way without using regex.. as long as there's always a single underscore preceding the number and as long as the number is 4 digits, this will work. 这是一种不使用正则表达式的快速方法。.只要在数字前始终有一个下划线,并且只要数字为4位数字,就可以使用。

 var n = 'MPG_0023'; var a = n.split('_'); var r = a[0]+'_'+(("0000"+(++a[1])).substr(-4)); console.log(r); 

Or if you do wanna do regex, the underscore won't matter. 或者,如果您想进行正则表达式,则下划线将无关紧要。

 var n = "MPG_0099"; var r = n.replace(/(\\d+)/, (match)=>("0".repeat(4)+(++match)).substr(-4)); console.log(r); 

You can use the regular expressions to make the changes as shown in the following code 您可以使用正则表达式进行更改,如以下代码所示

 var text = "MPG_0023"; var getPart = text.replace ( /[^\\d.]/g, '' ); // returns 0023 var num = parseInt(getPart); // returns 23 var newVal = num+1; // returns 24 var reg = new RegExp(num); // create dynamic regexp var newstring = text.replace ( reg, newVal ); // returns MPG_0024 console.log(num); console.log(newVal); console.log(reg); console.log(newstring); 

Using regex along with the function padStart 将正则表达式与padStart功能一起使用

 function add(str, n) { return str.replace(/(\\d+)/, function(match) { var length = match.length; var newValue = Number(match) + n; return newValue.toString(10).padStart(length, "0"); }); } console.log(add("MPG_023", 101)); console.log(add("MPG_0023", 101)); console.log(add("MPG_0000023", 10001)); console.log(add("MPG_0100023", 10001)); 

You could cast to number, increment the value and cast back. 您可以将其强制转换为数字,增加值并进行反向转换。 Then check if you need leading zeros by looking at the length of the string. 然后,通过查看字符串的长度来检查是否需要前导零。

Snippet below: 以下代码段:

 let str = "MPG_0023", num = Number(str.substr(4)) + 1, newStr = String(num); function addLeading0(str) { return str.length === 2 ? '00' + str : (str.length === 3 ? '0' + str : str); } console.log("MPG_" + addLeading0(newStr)); 

Using regular expression you can do it like this. 使用正则表达式,您可以这样做。

var text1 = 'MPG_0023';
var text2 = 'MPG_23';

var regex = /(.*_[0]*)(\d*)/;

var match1 = regex.exec(text1);
var match2 = regex.exec(text2);

var newText1 = match1[1] + (Number(match1[2]) + 1);
var newText2 = match2[1] + (Number(match2[2]) + 1);

console.log(newText1);
console.log(newText2);

Increment and pad the same value ( comments inline ) 递增填充相同的值( 内联注释

 var prefix = "MPG_" var padDigit = 4; //number of total characters after prefix var value = "MPG_0023"; console.log("currentValue ", value); //method for padding var fnPad = (str, padDigit) => (Array(padDigit + 1).join("0") + str).slice(-padDigit); //method to get next value var fnGetNextCounterValue = (value) => { var num = value.substring(prefix.length); //extract num value ++num; //increment value return prefix + fnPad(num, padDigit); //prepend prefix after padding }; console.log( "Next", value = fnGetNextCounterValue(value) ); console.log( "Next", value = fnGetNextCounterValue(value) ); console.log( "Next", value = fnGetNextCounterValue(value) ); 

One way would e to split the string on the "_" character, increment the number and then add the zeros back to the number. 一种方法是将字符串拆分为“ _”字符,递增数字,然后将零加回到数字上。

var testString = "MGP_0023";
var ary = testString.split("_");

var newNumber = Number(ary[1]) + 1;
var result = ary[0] + pad(newNumber);

// helper function to add zeros in front of the number
function pad(number) {
    var str = number.toString();
    while (str.length < 4) {
        str = '0' + str;
    }

    return str;
}

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

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