删除URL开头的string
我想从URLstring的开始处移除“ www. ”部分 
例如在这些testing案例中:
 例如www.test.com → test.com 
 如www.testwww.com → testwww.com 
 如testwww.com → testwww.com (如果不存在) 
我需要使用正则expression式还是有一个智能function?
取决于你需要什么,你有几个select,你可以做:
 // this will replace the first occurrence of "www." and return "testwww.com" "www.testwww.com".replace("www.", ""); // this will slice the first four characters and return "testwww.com" "www.testwww.com".slice(4); // this will replace the www. only if it is at the beginning "www.testwww.com".replace(/^(www\.)/,""); 
 如果string总是具有相同的格式,一个简单的substr()就足够了。 
 var newString = originalStrint.substr(4) 
手动,就像
 var str = "www.test.com", rmv = "www."; str = str.slice( str.indexOf( rmv ) + rmv.length ); 
 或者只是使用.replace() : 
 str = str.replace( rmv, '' ); 
是的,有一个RegExp,但你不需要使用它或任何“智能”function:
 var url = "www.testwww.com"; var PREFIX = "www."; if (url.indexOf(PREFIX) == 0) { // PREFIX is exactly at the beginning url = url.slice(PREFIX.length); } 
尝试以下
 var original = 'www.test.com'; var stripped = original.substring(4); 
您可以剪切url,并使用response.sendredirect(新url),这将带你到新url的同一页面