简体   繁体   中英

Having trouble with a search and replace using regular exrpession in Rails

I'm using Rails 4.2.3. I am trying got replace a parameter in my URL with the same value but adding “1” to it. So for instance if my URL is

http://replace.mydomein.com/profiles/152/expanded?embed=true&page=2

I would want the result after the substitution to be

http://replace.mydomein.com/profiles/152/expanded?embed=true&page=3

So I'm trying this

url = url.sub(/(\&<=page=)\d+/) {|m| m.to_i+1}

but it is having no effect on my URL. Passing “ http://replace.mydomein.com/profiles/152/expanded?embed=true&page=2 ” as an argument yields the same result. What am I doing wrong in my regular expression above?

You only need to replace the \\& in your regex to ?

url = url.sub(/(?<=page=)\d+/) { |m| m.to_i.next }

Because the syntax for a positive lookbehind (?<= is like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM