Javascript 如何使用正则表达式获取字符串的一部分并对其进行更改
例如,我想更改一个字符串-如果它的“Hello999”,它应该变成“Hello1000”,因此将数字增加1,但保留其余的数字。我想使用正则表达式来实现它,但我不知道如何从字符串中“取出”数字并增加它。 我的代码如下:Javascript 如何使用正则表达式获取字符串的一部分并对其进行更改,javascript,regex,Javascript,Regex,例如,我想更改一个字符串-如果它的“Hello999”,它应该变成“Hello1000”,因此将数字增加1,但保留其余的数字。我想使用正则表达式来实现它,但我不知道如何从字符串中“取出”数字并增加它。 我的代码如下: function stringNumber(str){ var string=""; if (/^.*\w+$/i.test(str)){ //testing if the end is a number,if not Ill add 1, not
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
})
将函数用作
.replace()
函数的第二个参数:
str.replace(/\d+/, function(match) {
return Number(match) + 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';
}
示例
函数stringNumber(str){
//替换字符串末尾的数字
return str.replace(/\d+$/,函数(n){return Number(n)+1;});
}
console.log(stringNumber('hello999'))代码>串接字符串与递增数字的比较
如果你想实际增加你的数字,你必须让它成为一个实际的数值开始(否则你将只是连接你的两个字符串)
考虑通过函数解析字符串的数字部分,然后在调用中递增它:
如果您想扩展此功能以处理字符串不包含数字的情况,也可以相当轻松地完成此操作:
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';
}
示例
函数stringNumber(str){
//替换字符串末尾的数字
return str.replace(/\d+$/,函数(n){return Number(n)+1;});
}
console.log(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
但是对于一个简单的任务来说太长了。无论如何,希望它能有所帮助:)
更新:我刚刚意识到,如果字符串不包含任何数字,您只需添加1。所以我只是稍微修改了一下。只是为了好玩
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
但是对于一个简单的任务来说太长了。无论如何,希望它能有所帮助:)
更新:我刚刚意识到,如果字符串不包含任何数字,您只需添加1。所以我只是修改了一下。你的号码可以在左边用零填充吗,比如“hello009”
?如果用户通过“hello”
,你会怎么做?你的号码可以在左边用零填充吗,比如“hello009”
?如果用户通过“hello”
,你会怎么做?谢谢。你能解释一下{returnnumber(n)+1;-到底发生了什么事吗?(我是一个初学者)。另外,我怎样才能将hello000转换成hello001而不是hello1?当然可以。returnnumber(n)
call实际上是从字符串中提取数值并将其转换为整数,这将允许您对其进行加法、减法等操作。处理零填充数字可能会让事情变得更棘手。哦,好吧。如果我这样做了,会不会是一样的:函数(n){parseInt(n)+1;})?基本上是的,尽管在前导零等方面,这两种方法有一些不同。此外,我还为您添加了一个使用前导零处理值的示例。您的代码运行正常,我几乎什么都不懂,因为您包含了大量复杂的内容,我是一个初学者!谢谢。你能解释一下{returnnumber(n)+1;-到底发生了什么事吗?(我是一个初学者)。另外,我怎样才能将hello000转换成hello001而不是hello1?当然可以。returnnumber(n)
call实际上是从字符串中提取数值并将其转换为整数,这将允许您对其进行加法、减法等操作。处理零填充数字可能会让事情变得更棘手。哦,好吧。如果我这样做了,会不会是一样的:函数(n){parseInt(n)+1;})?基本上是的,尽管在前导零等方面,本文讨论的这两种方法有一些不同。此外,我还为您添加了一个使用前导零处理值的示例。您的代码运行正常,但我几乎什么都不懂,因为您包含了大量复杂的内容,而我是一名初学者!这是什么意思[0]代表?它只是检索捕获的第一个组,因为match()
函数返回一个捕获的组数组。在“hello00123”
的情况下,第一个组是00123
。就是这样。如果您是指组([0]*)(\d+)中的[0]
,它只是表示一个与字符“0”
匹配的字符类。啊,好吧,[0]*(\d+)中的[0]实际上是指数字0,而不是第一个(零)捕获的地面,对吗?你是对的。但是,因为后面有一个*
,它将捕获所有的前导0
(如果有。*
表示0或更多)但最后一个0
不包括在内,因为它被(\d+
捕获到最后一位。0代表什么?它只检索捕获的第一个组,因为匹配()
函数返回捕获的组数组。。在“hello00123”
,第一组是00123
。就是这样。如果你是指([0]*)(\d+)(\code>)组中的[0]
,那么它只表示与字符“0”
匹配的字符类。啊好的,([0]*)(\d+)中的[0]实际上是指数字0,而不是第一个(零)捕获的地面,对吗?你是对的。但是因为它后面有一个*
,所以它将捕获所有领先的0
(如果有的话。*
表示0或更多),但最后一个