I have the following string: 0-3-terms and I need to increment the 3 by 20 every time I click a button, also the start value might not always be 3 but I'll use it in this example..
I managed to do this using substring but it was so messy, I'd rather see if it's possible using Regex but I'm not good with Regex. So far I got here, I thought I would use the two hyphens to find the number I need to increment.
var str = '0-3-terms';
var patt = /0-[0-9]+-/;
var match = str.match(patt)[0];
//output ["0-3-"]
How can I increment the number 3 by 20 and insert it back in to the str, so I get:
0-23-terms, 0-43-terms, 0-63-terms etc.
You're doing a replacement. So use .replace.
var str = '0-3-terms';
var patt = /-(\d+)-/;
var result = str.replace(patt,function(_,n) {return "-"+(+n+20)+"-";});
Another option is to use .split instead of regex, if you prefer. That would look like this:
var str = '0-3-terms';
var split = str.split('-');
split[1] = +split[1] + 20;
var result = split.join('-');
alert(result);
I don't understand why you are using regex. Simply store the value and create string when the button is called..
//first value
var value = 3;
var str = '0-3-terms';
//after clicking the button
value = value+20;
str = "0-" + value + "-terms"
Related
12:00:00:12
How to remove 6 character from the back? the output would be 12:00, I can't use substring to get the from the front to get the 6 char, because it can be 9:00 so it's just 4 char instead of 5.
I think #ZakariaAcharki is a better solution but if you want make it by substring try this:
str = '12:00:00:12';
str.substring(0,str.length-6);
I think better if you use split() function, and take the first and second items in splited array.
var my_string ="12:00:00:12";
var array_splited = my_string.split(':');
console.log( array_splited[0] + ':' + array_splited[1] ); //12:00
If you want it in single line, e.g :
my_string.split(':')[0] + ':' + my_string.split(':')[1];
Hope this helps.
You can determine the length and than go back 6 chars e.g.
str = '12:00:00:12'
str = str.substring(0,str.length - 6);
But you may better match with
str = '12:00:00:12'.match(/^[0-9]+:[0-9]+/)[0]
A regular expression with .match() method will do:
var str1 = '12:00:00:12';
var str2 = '9:40:00:12';
var regex = /(\d+)+:+(\d\d)/g;
var newStr1 = str1.match(regex)[0];
var newStr2 = str2.match(regex)[0];
document.querySelector('#one').textContent = JSON.stringify(newStr1);
document.querySelector('#two').textContent = JSON.stringify(newStr2);
'12:00:00:12' <pre id='one'></pre>
<hr>
'9:40:00:12' <pre id='two'></pre>
var str = "12:00:00:12";
var newStrArr = str.split(":");
newStrArr.pop();
newStrArr.pop();
newStrArr.join(":");
If the time will always be in the form (0-12):(00-59);(00-59) then you could use regex and the function .match() to get the time in the format you would like:
current_time = '12:00:00'
time_formatted = current_time.match(/\d+:\d+/)
Try using split and join.
EG 1:
var num = "12:00:00:12";
console.log(num.split(':', 2).join(':'));
EG 2:
var num = "9:00:00:12";
console.log(num.split(':', 2).join(':'));
Simple and best solution:
Use slice() function.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
$(function(){
var str = '12:00:00:12';
alert(str.slice(0,-6));
});
Output: 12:00
JSFiddle Demo
I have some code that matches a certain number of digits after a decimal. Currently, I have the following:
var input = getValueFromUser();
var count = getCount();
var x = Number(input.toString().match(/^\d+(?:\.\d{0,1})?/));
alert(x);
This approach always gets the first digit after the decimal. However, I want to replace the 1 in the regex with the value in count. How do I do that? I tried the following:
var pattern = '/^\d+(?:\.\d{0,' + count + '})?/';
var x = Number(input.toString().match(pattern));
However, now, I always get 0 for x.
You have to use Regexp object if you want to use dynamically built patterns:
var re = new RegExp('^\\d+(?:\\.\\d{0,' + count + '})?');
This will help you.
var pattern = '^\\d+(?:\\.\\d{0,' + '5' + '})?',
reg=new RegExp(pattern),
x = Number(input.toString().match(reg));
mask: new RegExp(`^[a-zA-Z0-9]{0,${maxLength}}$`)
its work for me
var alien = 'ajay'+' $%';
var all=new RegExp(`${alien}`)
I want to remove the last parameter in the href of a specific tag using jquery
for example replace href="app/controller/action/05/04/2014"
by href="app/controller/action/05/04"
Try using the String.lastIndexOf() and String.substring() in this context to achieve what you want,
var xText ="app/controller/action/05/04/2014";
xText = xText.substring(0,xText.lastIndexOf('/'));
DEMO
if you know which value you need to change ,than you can use replace:
var str = "app/controller/action/05/04/2014";
var res = str.replace("2014","04");
or else you can use array and change / update last value in array:
var str = "app/controller/action/05/04/2014";
var res = str.split("/");
We can use a regular expression replace which will be the fastest, compared to substring/slice.
var hreftxt ="app/controller/action/05/04/2014";
hreftxt = hreftxt.replace(/\/[^\/]*$/,"");
I want to extract the date and the username from string using .split() in this particular string:
var str ='XxSPMxX on 08/30/2012';
I want XxSPMxX in one variable and 08/30/2012 in the other.
Using just split:
var x = str.split('</a> on ');
var name = x[0].split('>')[1];
var date = x[1];
Demo: http://jsfiddle.net/Guffa/YUaAT/
I don't think split is the right tool for this job. Try this regex:
var str ='XxSPMxX on 08/30/2012',
name = str.match(/[^><]+(?=<)/)[0],
date = str.match(/\d{2}\/\d{2}\/\d{4}/)[0];
Here's the fiddle: http://jsfiddle.net/5ve7Y/
Another way would be to match using a regular expression, build up a small array to get the parts of the anchor, and then use substring to grab the date.
var str = 'XxSPMxX on 08/30/2012';
var matches = [];
str.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function () {
matches.push(Array.prototype.slice.call(arguments, 1, 4))
});
var anchorText = matches[0][2];
var theDate = str.substring(str.length - 10, str.length);
console.log(anchorText, theDate);
working example here: http://jsfiddle.net/dkA6D/
If I have a string... abcdefghi
and I want to use regex to load every elemnent into an array but I want to be able to stick anything connected by plus sign into the same element... how to do that?
var mystring = "abc+d+efghi"
output array ["a","b","cde","f","g","h","i"]
One way to do it:
var re = /([^+])(?:\+[^+])*/g;
var str = 'abcd+e+fghi';
var a = str.match(re).map(function (s) { return s.replace(/\+/g, ''); });
console.log(a);
The value of a[3] should now be 'def'.
http://jsfiddle.net/rbFwR/2
You can use this expression, to produce [a][b][c+d+e][f][g][h][i].
mystring.split ("(.\+)*.")
Next, replace any + characters with empty on the resulting list.
mystring.split("\\+")
Click here for more information.