Iladarsda
Iladarsda

Reputation: 10696

jQuery - trim the variable

I have following example:

var VarFull = $('#selectror').attr('href') where .attr('href') = "#tabs1-1"

How can I trim that to "tabs1-1" ( without #)??

Any suggestions much appreciated.

Upvotes: 0

Views: 910

Answers (6)

Alexei T
Alexei T

Reputation: 708

For example: ". / [email protected], / . \"

Now trim characters at the beginning and end of the string:

email_new = email.replace(/\W+$/g, '').replace(/^\W+/g, ''); // output : [email protected]

Upvotes: 0

Mahbub
Mahbub

Reputation: 3118

If it's certain that the url will contain # anyway, you can even split and take second element of array.

var trimmed=$('#selectror').attr('href').split("#")[1]

But don't use this if URL may not contain # otherwise you'll get an undefined error for trying to get index 1 of the array by split().

Upvotes: 1

StuperUser
StuperUser

Reputation: 10850

You can use JavaScript's string replace(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace

var VarFull = $('#selectror').attr('href');
var trimmed = VarFull.replace('#','');

Edit: This is a good article on JS string manipulation: http://www.quirksmode.org/js/strings.html

Upvotes: 2

Vivek
Vivek

Reputation: 11028

try this regEx with replace-

var VarFull = $('#selectror').attr('href').replace(/\#*/g, "");

it will replace all the # in your attr.

Upvotes: 1

ipr101
ipr101

Reputation: 24236

You could use replace -

var VarFull = $('#selectror').attr('href').replace('#','');

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074505

Use substring:

var VarFull = $('#selectror').attr('href').substring(1);

Upvotes: 5

Related Questions