繁体   English   中英

Javascript / jQuery字符串操作?

[英]Javascript/jQuery string manipulation?

我正在尝试仅捕获此URL的一部分或将字符串的一部分注入其中(以最简单的为准)。

这是网址:

http://www.goodbuytimeshare.com/listings/min:1000/max:150000/agreements:rent,buy/properties:apartment,condo,hotel,resort/show:4/

我认为我要么需要将其调整为:

/listings/min:1000/max:150000/agreements:rent,buy/properties:apartment,condo,hotel,resort/

或将其变成:

http://www.goodbuytimeshare.com/listings/ajax/min:1000/max:150000/agreements:rent,buy/properties:apartment,condo,hotel,resort/show:4/start:1/end:100/

(相同的URL,但将在“ .com /”之后添加“ ajax /”)

其中哪一个更简单?

简单,使用正则表达式!

var myString = "http://www.goodbuytimeshare.com/listings/min:1000/max:150000/agreements:rent,buy/properties:apartment,condo,hotel,resort/show:4/";
var matches = (/^http:\/\/[a-zA-Z0-9\.]+(\/.+)$/).exec(myString);
var mySubString = matches[1];

虽然, 现在您有两个问题 ;-)

这是非正则表达式解决方案。 当然,正则表达式是简洁的。

var original = "http://www.goodbuytimeshare.com/listings/min:1000/max:150000/agreements:rent,buy/properties:apartment,condo,hotel,resort/show:4/";

// split into an array of separate fields

var fields = original.split("/")

var results = "";

for (i=0;i < fields.length;i++) {
// Insert ajax/ in the fourth position

if (i == 3) {
   results += "ajax/";
}

results += fields[i] 

// don't put the slash on the last item

if (i < (fields.length -1)) {
   results += "/";
}
}

// write out the results and append more

document.write(results + "start:1/end:100/");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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