Increment a number in a string in with regex
Asked Answered
P

4

15

I get the name of an input element, which is a string with a number (url1). I want to increment the number by 1 (url2) in the easiest and quickest way possible.

My way would be to get \d / restofstring, ++ the match, then put together number with restofstring. Is there a better way?

Update:

My final (dummy)code became:

var liNew = document.createElement('li'); 
liNew.innerHTML = liOld.innerHTML; 
var els = Y.Dom.getChildrenBy(liNew, function(el) { 
    return el.name.match(/\d+$/); 
} // YUI method where the function is a test 
for (var i = 0, el; el = els[i]; i++) { 
    el.name = el.name.replace(/\d+$/, function(n) { return ++n }); 
} 
list.appendChild(liNew); 
Persis answered 16/11, 2009 at 15:13 Comment(2)
Er, you can't just change form element names like that in IE. You're gonna run into issues: #1651297Tart
Ok, thanks for the heads up. If I end up needing to support IE for this, I'll use YUI Element instead.Persis
T
44

How about:

'url1'.replace(/\d+$/, function(n){ return ++n }); // "url2"
'url54'.replace(/\d+$/, function(n){ return ++n }); // "url55"

There we search for a number at the end of the string, cast it to Number, increment it by 1, and place it back in the string. I think that's the same algo you worded in your question even.

Reference:

Tart answered 16/11, 2009 at 15:20 Comment(5)
Thanks, I updated the question with the solution I made with your examplePersis
This is a beautiful solution.Angrist
Here is a slightly modified version that works for all the numbers in a string: "url5part6".replace(/\d+/g, function(n){ return ++n }); //returns "url6part7"Anniceannie
Good answer but I fail to see the part where we "cast it to Number"Izolaiztaccihuatl
++ did that. It converts the operand to number implicitly and then increase that by one before returning the final value.Wreckfish
W
7

Simple. Use a substitution function with regular expressions:

s = 'abc99abc';
s = s.replace(/\d+/, function(val) { return parseInt(val)+1; });

will set variable s to: abc100abc

But it gets more complicated if you want to make sure you only change a certain parameter in the URL:

s = '?foo=10&bar=99';
s = s.replace(/[&?]bar=\d+/, function(attr) {
  return attr.replace(/\d+/, function(val) { return parseInt(val)+1; });
});

will set variable s to: ?foo=10&bar=100

Wire answered 16/11, 2009 at 15:19 Comment(3)
Hm, the question was not about get parameters?Persis
If you read the question carefully, the asker did not specify what exactly he wanted to change; not even that the number would be at the end of the URL -- he just used it in his example. I figured changing GET parameters would be a likely use case.Wire
"the name of an input element" = <input name="url1">Persis
T
2

You can use replace and pass it a function to use to replace the matched section:

str.replace(/\d+/, function(number) { return parseInt(number, 10) + 1; });
Twaddle answered 16/11, 2009 at 15:20 Comment(0)
C
1

Looks OK. You might want to use a regex like ^(.*?)(\d+)$, making sure the number you're grabbing is at the end of the string.

Curbing answered 16/11, 2009 at 15:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.