Add in value before single quote (') - javascript

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

Related

Replace match last instance of pattern in a string

I want to only show the value after the - as shown below.
$(function(){
var aa = "ABCDEFGHIJKLMNOPQRS-TUVEXYZ";
var bb = aa.substring(aa.indexOf("-") + 1);
$('#one').text(bb);
});
$(function(){
var cc = "ABC-DEFGHIJ-KLMNOPQRS-TUVEXYZ";
var dd = cc.substring(cc.indexOf("-") + 1);
$('#two').text(dd);
});
this outputs
<div id="one">TUVEXYZ</div>
<div id="two">DEFGHIJ-KLMNOPQRS-TUVEXYZ</div>
The first one works as there is only one dash however when there is multiple dashes my code doesn't work as it just looks for the first -. How would I go about looking for the last - only?
eg If my code was working like I want it to I would expect my end result to look like this.
<div id="one">TUVEXYZ</div>
<div id="two">TUVEXYZ</div>
Use lastIndexOf
var dd = cc.substring(cc.lastIndexOf("-") + 1);
Using regex:
var myregexp = /[^-]*$/;
var result = myregexp.exec(subject)[0];
This regex matches any number of non-dash characters, anchored to the end of the string, which effectively matches everything that follows after the last dash (or the entire string if there is no dash).
This might be the regex you are looking for :)
var.replace( /(?:.*)\-(.*?)$/, $1 ); // or "$1", not really shure in JS

how to get id from string which ends with certain word (regexp)

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.

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.
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

What regex can I use to extract the URL from an HTTP <a> element?

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");

how to extract string part and ignore number in jquery?

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,"");

Categories

Resources