开发者

String manipulation using MooTools / JavaScript

How do I manipulate a string using MooTools / JavaScript

I would like to replace开发者_Go百科 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


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


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.


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]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜