0

I have a string that contains a value that looks like this.

somevalueshere=123&page=3&someothervalues=123

and I want to replace the number 3 with 1. So it would look like page=1

The number is always a positive whole number like 1,2,3,4,5,6,7

All I have so far is

.replace("page=" + 'some number reg ex here', "page=1")
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • yes, sorry I updated my post – chuckd Jan 11 '17 at 22:38
  • 1
    Possible duplicate of [Using javascript replace to replace numbers in a string?](http://stackoverflow.com/questions/4178227/using-javascript-replace-to-replace-numbers-in-a-string) – Heretic Monkey Jan 11 '17 at 22:41
  • 1
    @MikeMcCaughan That doesn't look for a pattern before the number, it just replaces the number anywhere. – Barmar Jan 11 '17 at 22:51
  • So, the question is, do we want users to have to think or not? Or learn regex rather than coming back to SO every time? Apparently not. How about http://stackoverflow.com/q/1090948/215552? That will let them change any part of the query string they want. – Heretic Monkey Jan 11 '17 at 23:08

1 Answers1

3

The regular expression for a number (without decimals) is \d+. \d matches any numeric digit, and + means at least one of the preceding pattern.

str = str.replace(/\bpage=\d+/, 'page=1');

This is very basic regular expression syntax. If you don't already know it, you should read the tutorial at http://www.regular-expression.info.

Barmar
  • 741,623
  • 53
  • 500
  • 612