0

Here I have a line of code which I would prefer to take '123' as a string value.
Then with the help of split() and join() it will become a string like '1,2,3'.
Then I want to replace all the numeric string values with actual number like 1,2.....
For this I used a variable called k and give it an initial value 1.
I used an increment operator inside replace to change the value of k.
But it is not working. Maybe I am getting it wrong how string.replace() execute the replacement operation.
I also tried to use parseInt('$1') it also didn't work.

console gives output of 1,1,1.No increment operation takes place.

<html>
<body>
<script>

(function(){
var k=1;
   console.log(prompt('input here').split('').join(',').replace(/(\d)/g,k++));
})();
</script>
</body>
</html>
8
  • 1
    You mean you want to convert "123" into an array of numbers [1, 2, 3]? Commented Sep 12, 2014 at 12:48
  • yes.something like that BUT using a single line of code. Commented Sep 12, 2014 at 12:50
  • 1
    If you don't want to increment the numbers var arr = '123'.split('').map(Number); is the simplest solution I know. Commented Sep 12, 2014 at 12:56
  • 1
    var a = "123".split('').map(function(item) { item = parseInt(item, 10); return ++item; }); works for me Commented Sep 12, 2014 at 12:57
  • 1
    Or, upon re-reading, is it actually and OP doesn't want to create incrementing numbers? Commented Sep 12, 2014 at 12:59

2 Answers 2

3

Well, you can use replace callback instead:

var k = 1;
'123'.split('').join(',').replace(/\d/g, function() {
  return k++;
});

As it stands, k++ value is taken as an argument - in other words, this operation is evaluated just once.

But if you want just to get a sequence of integers, there are far better approaches. For example:

function getSequence(start, end) {
  var res = [];
  for (var i = start; i <= end; i++) {
    res.push(i);
  }
  return res;

  // or just...
  // return Array.apply(0, Array(end - start + 1))
  //             .map(function(_, i) { return start + i });
  // ... if you feel adventurous. )
}
getSequence(2, 4); // [2, 3, 4]
Sign up to request clarification or add additional context in comments.

9 Comments

You're relieving me from having to write my own answers :-)
Won't this only work for a string of the form "1234..."? I think the one in OP's post was just an example.
@JLRishe No, it replaces any digit in the number by the iterated index (leaving the non-digit symbols as they are).
Because k++ is just an expression, not a command. replace function will receive the result of this expression as its parameter - it won't have any idea about how you've come with this result.
The callback will be invoked each time a replacement operation is in progress; its return value for the given invokation will be used as a replacement value. That's why it's named callback, after all.
|
2

If you don't want to increase the numbers:

var arr = '123'.split('').map(Number);

If you do:

var arr = '123'.split('').map(function (num) { return Number(num) + 1; });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.