繁体   English   中英

如何使用正则表达式将字符串的一部分取而代之

[英]How can I take one part of a string using regex and change it

我想更改一个字符串,例如,如果它的“ Hello999”应该变成“ Hello1000”,因此将数字增加一个,但保留其余部分。 我想使用正则表达式来做,但是我不确定如何只从字符串中“取出”数字并增加它。 我的代码如下:

function stringNumber(str){
    var string="";
    if (/^.*\w+$/i.test(str)){ 
        //testing if the end is a number,if not Ill add 1, not increment
        string=str+1;
        return string;
    }
    else if (/^.*\w+\d+$/i.test(str)){
        string=0;
        string+=1;
        //thats wrong because I keep adding a 1, not incrementing
        return string;
    }
}
stringNumber('hello999');

更新版本://也无法正常工作

function stringNumber(str){
 var string="";
 if (/^.*\w+$/i.test(str)){ 
    string=str+1;
    return string;
 }
 else if (/^.*00\d+$/i.test(str)){
    string=str.replace(/0\d+$/, function(n) { return parseInt(n) + 1; });
//trying to keep 1 zero and replace the second zero PLUS the number following with incremented number
    return string;
 }

}
stringNumber("hello00999");

使用一个函数作为.replace()函数的第二个参数:

str.replace(/\d+/, function(match) {
  return Number(match) + 1
})

串联字符串与递增数字

如果要实际增加数字,则必须将其设为一个实际的数字值(否则,您将只连接两个字符串)。

考虑只通过Number()函数解析字符串的数字部分,然后在replace()调用中递增它:

function stringNumber(str){
     // Replace the number at the end of the string 
     return str.replace(/\d+$/, function(n) { return Number(n) + 1; });
}

如果您想扩展它以处理字符串中不包含数字的情况,则也可以很容易地完成此操作:

function stringNumber(str){
     // Replace the number at the end of the string or append a 1 if no number
     // was found
     return /\d+$/.test(str) ? str.replace(/\d+$/, function(n) { return Number(n) + 1; }) : str + '1';
}

 function stringNumber(str) { // Replace the number at the end of the string return str.replace(/\\d+$/, function(n) { return Number(n) + 1; }); } console.log(stringNumber('hello999')); 

更新:现在带有填充字符串

既然听起来您需要处理带填充的字符串,则可以通过一个简单的填充函数并通过检查一些边缘情况来确定何时需要填充额外的数字来完成此操作:

function stringNumber(str) {
  // Replace the number at the end of the string or add a padded version of your number
  return /\d+$/.test(str) ? str.replace(/\d+$/, function(n) {
      // Determine if we are looking at an edge (i.e. all 9s)
      var rangeToPad = /^9+$/.test(n) ? n.length + 1 : n.length;
      // Pad our result by the specified amount
      return pad(Number(n) + 1, rangeToPad);
    })
    // Or simply add a one to the value (padded by three digits)
    : str + pad(1, 3);
}

function pad(num, size) {
  return ('00000' + num).substr(-size);
}

填充示例

 function stringNumber(str) { // Replace the number at the end of the string or add a padded version of your number return /\\d/.test(str) ? str.replace(/\\d+$/, function(n) { // Determine if we are looking at an edge (ie all 9s) var rangeToPad = /^9+$/.test(n) ? n.length + 1 : n.length; // Pad our result by the specified amount return pad(Number(n) + 1, rangeToPad); }) // Or simply add a one to the value (padded by three digits) : str + pad(1, 3); } function pad(num, size) { return ('00000' + num).substr(-size); } console.log('hello => ' + stringNumber('hello')); console.log('hello001 => ' + stringNumber('hello001')); console.log('hello998 => ' + stringNumber('hello998')); console.log('hello999 => ' + stringNumber('hello999')); 

纯娱乐...

function stringNumber(str){
    if (str.match(/\d+/)){
      var t = str.match(/\d+/)[0];
      var n = parseInt(t) + 1;
      return str.replace(/([0]*)(\d+)/, "$1" + n);
    }
    return str+"1";
}
console.log(stringNumber("hello00123")); // hello00124
console.log(stringNumber("hello1236")); // hello1237
console.log(stringNumber("hello00")); // hello01
console.log(stringNumber("hello")); // hello1

但是对于一个简单的任务来说太长了。 随处可见,希望对您有所帮助:)

UPDATE :我刚刚意识到,如果字符串不包含任何数字,则只需加1。 所以我只是修改了一下。

暂无
暂无

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

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