How to increment only digits of string? [duplicate] - javascript

This question already has answers here:
How to increment number in string using Javascript or Jquery
(6 answers)
Closed 6 years ago.
I may have following type strings
A1 or 1A or AB....1 or 1AB......
so how to increment only digits of above type of strings in javascript?
var adminNo = data.Admission_No.slice(-2);
alert(adminNo);
var removedNo = data.Admission_No.substring(data.Admission_No.length-1);
alert(removedNo);

Use the replace method as shown in demo below
function incrementer(input)
{
return input.replace(/\d+/, function(match){ return parseInt(match) + 1 });
}
alert(incrementer("A1"));
alert(incrementer("1A"));
This will find the integer anywhere in the input string and increment it by one.

string.replace(/\d+/, function(n){ return ++n });

You can do it by taking out integer from your string
Its big long, but more self-explainatory
var youroriginalstring="A1"
var withNoDigits = youroriginalstring.replace(/[0-9]/g, '');
var yournumber = youroriginalstring.replace ( /[^\d.]/g, '' );
var incNos=yournumber +1;
var newString = incnos + "withNoDigits"

Related

How to create a function to split the following string? [duplicate]

This question already has answers here:
What is the shortest function for reading a cookie by name in JavaScript?
(20 answers)
Closed 2 years ago.
I have this string
"G_ENABLED_IDPS=app; COOKIE_EINF=someCookie; _ga=someGA;
_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5"
And i need some sort of function to split this string given an argument , for example given that my string is text
text.split(";") , will split it into an array separating it by ";"
But i need a function like this
returnText(text , property) that would work like
returnText(text, "_gcl_au") --> returns "someglcau"
You could actually use a regex replacement approach here, for a one-liner option:
function returnText(text, property) {
var term = text.replace(new RegExp("^.*\\b" + property + "=([^;]+)\\b.*$", "gm"), "$1");
return term;
}
var input = "G_ENABLED_IDPS=app; COOKIE_EINF=someCookie;_ga=someGA;_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5";
console.log(returnText(input, "_gcl_au"));
you can use split, just as you tried:
function returnText(text , property){
entries = text.split('; ');
const newEntries = [];
entries.forEach(item => {
let vals = item.split('=');
newEntries[vals[0]] = vals[1]
});
return newEntries[property];
}
const text = "G_ENABLED_IDPS=app; COOKIE_EINF=someCookie; _ga=someGA;_hjid=someHJID; _gcl_au=someglcau; COOKIE_EINF_SESS=somecookie1; _gid=somegid; _hjIncludedInPageviewSample=2; _hjTLDTest=3; _hjAbsoluteSessionInProgress=0; _hjIncludedInSessionSample=1; _gat_UA-124355-12=5";
console.log(returnText(text,'_gcl_au'));

JS get the last dot of a string and return the string after it [duplicate]

This question already has answers here:
String manipulation - getting value after the last position of a char
(8 answers)
Closed 5 years ago.
I have this RegExp below working. But the return is True or False. What I want here is to get the last DOT of a string and return the last string after the DOT. As example below I have image.jpeg.jpeg I want to return the last jpeg. I also try g.match(x) but it gives me an error. g.match is not a function
var x = "image.jpeg.jpeg"
var g = /(.*)\.(.+)/;
alert(g.test(x));
Alternatively, you can use split method like so:
var x = "image.jpeg.jpeg";
var ans = x.split('.').pop();
console.log(ans);
Try this:
var x = "image.jpeg.jpeg"
var g = /(.*)\.(.+)/;
alert(x.match(g)[2]);
match is a method of String (not RegExp), it's argiment RegExp
Try this:
var matches = x.match(g);
if (matches.length === 0) {
console.log('Error');
return;
}
var last = m[m.length - 1]
alert(last);
use this:
var x = "image.jpeg.jpeg"
var g = /\.([0-9a-z]+)$/i;
console.log(x.match(g)[0]); //with a dot
console.log(x.match(g)[1]); //extension only

How to replace the 3rd character and multiple of 3 's character in a string with a \n in javascript [duplicate]

This question already has answers here:
How can I insert a character after every n characters in javascript?
(9 answers)
Closed 5 years ago.
I have a string like 'oeew9w79WMIGL'. I want to get an output like below,
oee
w9w
79W
MIG
L
How to do it with plain JavaScript?
You could replace a part of the string.
var string = 'oeew9w79WMIGL';
console.log(string.replace(/.../g, '$&\n'));
if you use regex
let str = 'oeew9w79WMIGL';
document.write(str.match(/.{1,3}/g));
$(document).ready(function(){
var foo = "oeew9w79WMIGL";
$('#test').html(foo.match(/.{1,3}/g).join("<br />") );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="test">
</span>
var str= "oeew9w79WMIGL";
console.log( str.match(/.{1,3}/g).join("<br />") );
each_slice = function(enm, slice_size) {
var results = [];
slice_size = (slice_size < 1 ? 1 : slice_size );
for (var i=0; i<=enm.length; i=i+3) {
results.push(enm.slice(i, i+3));
}
return results;
}
var x = "oeew9w79WMIGL";
each_slice(x,3).join("\n")
//oee
//w9w
//79W
//MIG
//L
for reference: https://stackoverflow.com/a/41219012/4481312

How can I extract only the integer from a String JavaScript [duplicate]

This question already has answers here:
How to find a number in a string using JavaScript?
(9 answers)
Closed 6 years ago.
I have a string that looks like:
var a = "value is 10 ";
How would I extract just the integer 10 and put it in another variable?
You could use a regex:
var val = +("value is 10".replace(/\D/g, ""));
\D matches everything that's not a digit.
you can use regexp
var a = "value is 10 ";
var num = a.match(/\d+/)[0] // "10"
console.log ( num ) ;
You can use some string matching to get an array of all found digits, then join them together to make the number as a string and just parse that string.
parseInt(a.match(/\d/g).join(''))
However, if you have a string like 'Your 2 value is 10' it will return 210.
You do it using regex like that
const pattern = /\d+/g;
const result = yourString.match(pattern);

How can I parse a string and split it into two parts at a special character with javascript? [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
How can I take a string and split it at a special character into two new variables (and remove the special chars) with javascript?
For example take:
var X = Peggy Sue - Teacher
and turn it into:
varnew1 = Peggy Sue
varnew2 = Teacher
I guess it should also include a condition... if the string has a "-" then do this.
.split is probably what you want. Here is a very simple example
JSFiddle Link
var string = 'Peggy Sue - Teacher'
var new1 = string.split('-')[0].trim();
var new2 = string.split('-')[1].trim();
console.log(new1); // "Peggy Sue"
console.log(new2); // "Teacher"
And if you want to place a simple condition on it looking for - you can do so with the following
var string = 'Peggy Sue - Teacher'
var new1 = string.indexOf('-') !== -1 ? string.split('-')[0].trim() : string
var new2 = string.indexOf('-') !== -1 ? string.split('-')[1].trim() : string
Second Fiddle
var result = str.split("-");
will give you an array with 2 members,
result[0] = Peggy Sue
result[1] = Teacher

Categories

Resources