[英]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.