chuckd
chuckd

Reputation: 14520

How to replace a string with a number with another string number

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")

Upvotes: 0

Views: 75

Answers (1)

Barmar
Barmar

Reputation: 780713

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.

Upvotes: 3

Related Questions