Aadi
Aadi

Reputation: 7109

String manipulation using MooTools / JavaScript

How do I manipulate a string using MooTools / JavaScript

I would like to replace all after /p/ in following url:

http://example.com/groups/browse/catId/14/p/1000-1500

Expected result:

 http://example.com/groups/browse/catId/14

Upvotes: 1

Views: 1774

Answers (3)

Guffa
Guffa

Reputation: 700382

That doesn't look like you want to replace, it looks like you want to remove.

You can do that with regular string operations in plain Javascript:

var s = "http://example.com/groups/browse/catId/14/p/1000-1500";

s = s.substr(0, s.indexOf("/p/")));

Upvotes: 4

Mihai Iorga
Mihai Iorga

Reputation: 39704

you don't have to use mootools necessarily. you can split the string with split function from javascript

var myString = 'http://example.com/groups/browse/catId/14/p/1000-1500';
newString = myString.split('/p/');
alert(newString[0]);

Upvotes: 0

Andy E
Andy E

Reputation: 344585

Simple enough with regex:

var url = "http://example.com/groups/browse/catId/14/p/1000-1500";

console.log(url.replace(/\/p\/.+/, ""));
//-> "http://example.com/groups/browse/catId/14"

In the regex above, \/p\/ is /p/ with escaped slashes, followed by .+ which means match any character (except white space) one or more times.

You can brush up on your JavaScript regular expressions at http://www.regular-expressions.info/javascript.html.

Upvotes: 2

Related Questions