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.
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
Related
I want to add in value before the end of a single quote. Please do help me thanks!
var example = [['2019-02-10', 6.2]],[['2019-03-10', 6.2]]
After replacement
var example = [['2019-02-10 0:00AM' , 6.2]], [['2019-03-10 0:00AM' , 6.2]]
I found this code but unfortunately only for the comma.
var er = rep.replace(/\s*,\s*/g, " 0:00AM , ");
You need some regexp to make it work, please try this
your RegExp should look like this
var regexp = /(\'[a-zA-Z_0-9-]+)\'?/g;
so you should have a string like this:
var st = "[['2019-02-10', 6.2]],[['2019-03-10', 6.2]]";
then you can replace the desired content like this
st.replace(regexp, "$1 0:00AM'")
notice that $1 represent the first match regexp against the string
so finally you should have it like this
"[['2019-02-10 0:00AM', 6.2]],[['2019-03-10 0:00AM', 6.2]]"
I hope it can help you
Given /foo/bar/image.jpg?x=1&y=2, how do I obtain image.jpg?
https://stackoverflow.com/a/423385 provides a partial answer, but does not address the GET parameters.
You can use regex as others have suggested, but I find this more readable:
var src = '/foo/bar/image.jpg?x=1&y=2';
var img = src.split('/').pop().split('?')[0];
console.log(img);
Since the ? symbol separates the GET parameters from the URL, you can
str=str.split("?")[0]
filename = str.replace(/^.*[\/]/, '')
Try:
var str = "/foo/bar/image.jpg?x=1&y=2";
var fileName = str.split('/').slice(-1)[0].split('?')[0];
or, for a regex method:
var fileName = str.split('/').slice(-1)[0].match(/[^?]+/)[0];
You can use this regex:
/\/([^?\/]+(?=\?|$))/
and use captured grpup #1.
RegEx Demo
/ # matches a literal /
[^?\/]+ # matches 1 or more of any char that is not ? or /
(?=\?|$) # is a lookahead to assert that next position is ? or end of line
i want to get id which is ends with "_theTable" in string using regex. but i am not getting that . i am using this code:-
var str="<table id='dnn_ctl_123_theTable'><tr><td></td></tr></table>";
var rexexp = new RegExp("\b\w*_theTable\b");
var matchedwrd=rexexp.exec(str);
Please guide how to do this?
Thanks in Advance
When you use a new Regexp you have to escape your backslashes like so:
var rexexp = new RegExp("\\b\\w*_theTable\\b");
Or you can use a regex literal:
var rexexp = /\b\w*_theTable\b/;
var str="<table id='dnn_ctl_123_theTable'><tr><td></td></tr></table>"
var rexexp = /id='(.+?)_theTable'/;
var matchedwrd=rexexp.exec(str);
alert(matchedwrd[1]);
var str="<table id='dnn_ctl_123_theTable'><tr id='another'><td></td></tr></table>";
var regEx = /id='(.*?_theTable)'/;
var id = str.match(regEx)[1];
document.write(id);
I'd probably use the pattern id='([^']*)_theTable' for this. Then $1 should correspond to the portion of the id before _theTable. If you want to include the _theTable bit, just move the closing parenthesis after it.
Here is the Original string :
var str = " a vartiable";
and I need this part:
str = "https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&jobId=385594&type=hotjobs&JobReqLang=141&JobSiteId=5239&JobSiteInfo=385594_5239&GQId=0";
In other words, I need to remove the tag <a> and the document.href value
Thanks guys.
How about:
var str = " a vartiable";
str.replace(/^<a href="(https.*?)cim_home\.asp.*?'(cim_jobdetail\.asp.*)'.*$/, "$1$2");
produces:
"https://sjobs.brassring.com/1033/ASP/TG/cim_jobdetail.asp?SID=^cJgiKPhGBHyn5VRSb9gbJg0K2T88FrLqHyAtd6hd5pJ7JeXxNyq0VatKCq3jYWp/&jobId=385594&type=hotjobs&JobReqLang=141&JobSiteId=5239&JobSiteInfo=385594_5239&GQId=0"
Something simple like the following should work...
href="(.*?)"
here's the code u want:
var str = ' a vartiable'
var url = /\"(.*?)\"/str
that's how you match, here's how you strip it out:
str.replace(/\"(.*?)\"/, "$1");
the \"(.*?)\" gives the first minimal set of characters between two " characters the id of $1 then the second argument to the replace function tells it to replace the whole string with what's contained in $1
Also, if you use jQuery, this becomes pretty trivial:
var url = $("a").attr("href");
I have a string like foobar1, foobaz2, barbar23, nobar100 I want only foobar, foobaz, barbar, nobar and ignoring the number part.
If you want to strip out things that are digits, a regex can do that for you:
var s = "foobar1";
s = s.replace(/\d/g, "");
alert(s);
// "foobar"
(\d is the regex class for "digit". We're replacing them with nothing.)
Note that as given, it will remove any digit anywhere in the string.
This can be done in JavaScript:
/^[^\d]+/.exec("foobar1")[0]
This will return all characters from the beginning of string until a number is found.
var str = 'foobar1, foobaz2, barbar23, nobar100';
console.log(str.replace(/\d/g, ''));
Find some more information about regular expressions in javascript...
This should do what you want:
var re = /[0-9]*/g;
var newvalue= oldvalue.replace(re,"");
This replaces al numbers in the entire string. If you only want to remove at the end then use this:
var re = /[0-9]*$/g;
I don't know how to do that in JQuery, but in JavaScript you can just use a regular expression string replace.
var yourString = "foobar1, foobaz2, barbar23, nobar100";
var yourStringMinusDigits = yourString.replace(/\d/g,"");