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
4 Answers
Reset to default 5You 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.