Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to Perform Operations on Regex Backreference Matches in Javascript?
  2. var string = "This is a string with numbers 1 2 3 4 5 6 7 8 9 10";
  3.  
  4. var desiredResult = "This is a string with numbers 2 3 4 5 6 7 8 9 10 11";
  5.        
  6. string.gsub(/(d+)/) { "#{$1.to_i + 1}"}
  7.        
  8. var x = "This is x1, x2, x3.";
  9. var y = x.replace(/x(d+)/g, function(m, g1) {
  10.   return "y" + (Number(g1)+1);
  11. });
  12. y; // => "This is y2, y3, y4."
  13.        
  14. var string = "This is a string with Numbers 1 2 3 4 5 6 7 8 9 10";
  15. var desiredResult = "This is a string with Numbers 2 3 4 5 6 7 8 9 10 11";
  16. var actualResult = string.replace(/([0-9]+)/g, function() {
  17.     return parseInt(arguments[1])+1
  18. });
  19. console.log(actualResult)