最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ltrim with jQuery? - Stack Overflow

programmeradmin1浏览0评论

I am using jQuery to capture an encoded URL.

I looks something like: #?var=asdasdasdasdasd

I need to strip "#?" at the beginning of the string. In PHP I'd use ltrim. How can I do this with jQuery or plain JS?

 $("#myLink").click(function(){
       var myUrl = $(this).attr("href");
       var cleanUrl = ....

 }

I am using jQuery to capture an encoded URL.

I looks something like: #?var=asdasdasdasdasd

I need to strip "#?" at the beginning of the string. In PHP I'd use ltrim. How can I do this with jQuery or plain JS?

 $("#myLink").click(function(){
       var myUrl = $(this).attr("href");
       var cleanUrl = ....

 }
Share Improve this question asked Jul 17, 2012 at 1:46 santasanta 12.5k51 gold badges161 silver badges266 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

You could use replace method.

var cleanUrl = myUrl.replace(/^#\?/, '');

You could just use substr(2). It's more efficient.

Here are three ways to solve your problem.

$("#myLink").click(function(){
   var myUrl = $(this).attr("href");
   var cleanUrl = ...??? // what goes here.
}

Method 1: Use String.prototype.replace.

var cleanUrl = myUrl.replace( /^#\?/, "" );

Method 2: Use String.prototype.split

var cleanUrl = myUrl.split( "#?" ).pop();

Method 3: Use String.prototype.match

var cleanUrl = (myUrl.match( /^#\?(.)*/ )||[myUrl]).pop();

Extra Method:

var i = myUrl.indexOf( "#?" );
i = ( i < 0) ? 0 : i + 2;
var cleanUrl = myUrl.substring( i );

var trim = values.trim();

this will remove all the whitespaces.

发布评论

评论列表(0)

  1. 暂无评论