Replace all occurences of "-" between numbers with ":" in javascript - javascript

I have a string which looks like this:
1,87-99,88:12,sds-554,sdsd,787,99-145
If a "-" appears in the middle of 2 numbers, I want to change it to a ":".
What is the shortest way to do this in javascript? Thanks for help.

try:
text='1,87-99,88:12,sds-554,sdsd,787,99-145';
tex2=text.replace(/(\d)-(\d)/g,'$1:$2');
console.log(tex2);
example: http://jsfiddle.net/bingjie2680/Wcptr/

Here's my variant.
var a = '1,87-99,88:12,sds-554,sdsd,787,99-145'
var b = a.replace(/(\d)-(\d)/g,'$1:$2')
$1 and $2 puts matched digits back into string.

Try
'1,87-99,88:12,sds-554,sdsd,787,99-145'.replace(/(\d)-(\d)/g, '$1:$2'));​
http://jsfiddle.net/ZK9Mj/

var a = '1,87-99,88:12,sds-554,sdsd,787,99-145'
var b = a.replace(/(\d)-(\d)/g, '$1:$2');
"b" will give you what you want

Related

Add in value before single quote (')

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

How to string replace and string re arrange

I have a string that returns 04/01/2014 , my problem now is how can I change it using jquery so that the string will become 2014-04-01 ?
var stringDate = '04/01/2014';
any help will be appreciated, thanks in advance
You can split a JavaScript string into parts delimited by a pattern (in your case, /), and you can combine them back together however you like using another delimiter.
For instance:
var str = "04/01/2014";
var parts = str.split("/");
var result = parts[2] + "-" + parts[0] + "-" + parts[1];
make a DateTime() variable then you can manipiulate it how you want

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

What is the correct pattern for splitting a string in javascript, leaving just a-z words

I have the next code that was given to me to split up a string into an array.
var chk = str.split(/[^a-z']+/i);
The problem I'm having with this solution is that if the string has a period in the end, it's being replaced with ","
For example:
If I have the next string: "hi,all-I'm-glad."
The solution above results: "hi,all,I'm,glad," (notice the "," in the end).
I need that the new string will be: "hi,all,I'm,glad"
How can I acheive it ?
Check for a . being the last character and remove it first
var str = "hi,all-I'm-glad. that you, can help,me. that-doesn't make any-sense, I know.";
if(str.charAt( str.length-1 ) == ".") {
str = str.substring(0,str.length-1);
}
var chk = str.split(/[^a-z']+/i);
console.log(chk);
var chk = str.match(/[a-z']+/gi);
console.log(chk);
You could check to see if the last element of your string array returns an empty string and remove that element
if (chk[chk.length-1] == "")
{
chk.pop();
}
var chk="to.to.".split(/[^a-z']+/i); if(chk[chk.length-1].length==0){chk.pop()}; console.log(chk);
To remove the last value of your array using pop if this one is empty.
You can utilize the pure regex power:
"hi,all-I'm-glad. that you, can help,me. that-doesn't make any-sense, I know.".replace(/[\-\.\s]/g, ',').replace(/,{2,}/g, ',').replace(/,$/,'')

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

Categories

Resources