Lucas Puerari
Lucas Puerari

Reputation: 141

Replace forward slash with jQuery

This is my string:

hello/page/2/bye

This is what I would have (please note that "2" could be any number, "page" is always "page":

hello/bye

How can I do with str.replace() using jQuery?

Upvotes: 0

Views: 433

Answers (1)

Reeno
Reeno

Reputation: 5705

This will do it (without jQuery):

var str = 'hello/page/2/bye';
str = str.replace(/page\/\d+\//,'');

The slashes need to be escaped (so they become \/), \d+ is any number, one or more times (so it also matches e.g. page/12/

Demo: http://jsfiddle.net/dd5aohbc/

Upvotes: 2

Related Questions