jQuery - trim the variable
I have following example:
var VarFull = $('#selectror').attr('href')
where 开发者_JAVA技巧.attr('href') = "#tabs1-1"
How can I trim that to "tabs1-1"
( without #
)??
Any suggestions much appreciated.
Use substring
:
var VarFull = $('#selectror').attr('href').substring(1);
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
You could use replace
-
var VarFull = $('#selectror').attr('href').replace('#','');
try this regEx with replace-
var VarFull = $('#selectror').attr('href').replace(/\#*/g, "");
it will replace all the # in your attr.
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().
For example: ". / email@gmail.com, / . \
"
Now trim
characters at the beginning and end of the string:
email_new = email.replace(/\W+$/g, '').replace(/^\W+/g, ''); // output : email@gmail.com
精彩评论