简体   繁体   English

在javascript中文本字符串内的数字加1

[英]Add 1 to number inside of text string in javascript

I have the following variable in JavaScript: 我在JavaScript中具有以下变量:

var variable = "a=1; b=2; c=14;"

How can I change b=2 to b=3 , b=4345 to b=4346 , so adding 1 to the number after b= ? 如何将b=2更改为b=3 ,将b=4345更改为b=4346 ,所以在b=之后将数字加1?

You can do the following: 您可以执行以下操作:

var variable = "a=1; b=2; c=14;"

function incB(str) {
  var re = /(.*b=)(\d+)(.*)/, // split the string to the whole match and 3 groups
      a = re.exec(str); // <- ["a=1; b=2; c=14;", "a=1; b=", "2", "; c=14;"]
  return a[1] + (parseInt(a[2], 10)+1) + a[3];
}

variable = incB(variable); // <- variable will be "a=1; b=3; c=14;"

It may be a bit messy but you can easily utilize the .split() function. 它可能有点混乱,但是您可以轻松地使用.split()函数。 Here's an example : 这是一个例子:

var num = "a=1; b=2;";
var indSection = num.split(";");
var b = indSection[1].split("=");
document.write(++b[1]);

You can use split() function twice - first to separate those letters with their values and second time on those separated parts to separate letter from value. 您可以使用split()函数两次-首先将这些字母及其值分开,第二次在这些分开的部分上将字母与值分开。 After that you can add 1 to each number and build the string again by concatenating all those parts. 之后,您可以为每个数字加1,然后通过连接所有这些部分来再次构建字符串。

You can also use regular expressions . 您还可以使用正则表达式

Could use a regular expression to parse the value out, and replace the original string with the new value, something like this: 可以使用正则表达式解析出该值,然后用新值替换原始字符串,如下所示:

var variable = "a=1; b=2; c=14;"
var reg = /b=([0-9])+/;
var i = parseInt(reg.exec(variable)[1]);

i++;

variable = variable.replace(reg, "b="+i);
console.log(variable);

This should do it: 应该这样做:

variable = variable.replace(/(b=)(\d+)(?=;)/g, function(_, pref, num) {
    return pref + (+num + 1);
});

I am against writting code, so here are some hints: 我反对编写代码,因此这里有一些提示:

  1. remove all whitespaces str = str.replace(/\\s/g,""); 删除所有空格str = str.replace(/\\s/g,"");
  2. create an array arr = str.split(";"); 创建一个数组arr = str.split(";");
  3. loop on all elements of the array for(i=0, len=arr.length; i<len; i++) 在数组的所有元素上循环for(i=0, len=arr.length; i<len; i++)
  4. assing tmp to arr[i].split("=")[1] Note: [1] takes the right part, [0] the left part tmparr[i].split("=")[1]注意: [1]占据右侧, [0]占据左侧
  5. change tmp type from string to number with tmp = tmp*1 使用tmp = tmp*1tmp类型从字符串更改为数字
  6. update arr[i] with arr[i]=arr[i].split("=")[0]+""+(tmp+1) arr[i]=arr[i].split("=")[0]+""+(tmp+1)更新arr[i]
  7. join arr.join(" ;") to create your string again 加入arr.join(" ;")再次创建您的字符串

It seems you only want to change b= , so 看来您只想更改b= ,所以 inside the loop, make an if check on the left part and test it against "b" , however 在循环内部,在左侧部分进行if检查,并针对"b"测试 it would be easier to split by ;b= or ; b= ;b=; b=分割会更容易; b= ; b= , then take the right part, then split it by ; ; b= ,然后取正确的部分,然后除以; and take the left part, etc, etc. Like this answer . 并取左部分,依此类推

You could split the string on 'b=' and the first following semicolon, use parseInt() on b and glue it all back together. 您可以在'b ='和后面的第一个分号上分割字符串,在b上使用parseInt()并将其重新粘合在一起。

var splitted = variable.split('b=');
var begin = splitted[0];                   //everything up to b=
var end = splitted[1];                     //everything from b= on
var b = parseInt(end.substr(0, end.indexOf(';')))+1;   //b+1
variable = begin+"b="+b+end.substr(end.indexOf(';'));

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

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