How to extract ID from string in Javascript - javascript

I got a string
var str = "javascript:DeleteCountryConfirm(191)";
and I need to have the ID 191 in a variable. How to do it?

In case it's actually a string like this:
var str = "javascript:DeleteCountryConfirm(191);"
You can do this:
var id = parseInt(str.match(/\d+/)[0], 10);
Or:
var id = parseInt(str.replace(/[^0-9-]/, ''), 10)

Related

How to get remaining string after substring deletion in JavaScript?

I have a string 123456789.
My aim is to get 1236789 after deleting 45.
I used slice method to delete the string 45.
var x = 123456789;
res = x.slice(4, 5); //my output is 45
I need the output to be 1236789
Any help would be appreciated.
Slice before, slice after, and concatenate the resulting strings or replace the 45 with an empty string:
var x = "123456789"
var res1 = x.slice(0, 3) + x.slice(5)
console.log(res1)
var res2 = x.replace('45', '')
console.log(res2)
try replace/2.
"123456789".replace("45", "") /** 1236789 */
Or, if your input is an integer and you need and integer as an outcome,
var a = 123456789;
parseInt(a.toString().replace("45", ""))
You can so this:
var x = "123456789";
var result = x.substr(0, 3) + x.substr(5);
console.log(result)

replace first n occurrence of a string

I have a string var with following:
var str = getDataValue();
//str value is in this format = "aVal,bVal,cVal,dVal,eVal"
Note that the value is separated by , respectively, and the val is not fixed / hardcoded.
How do I replace only the bVal everytime?
EDIT
If you use string as the regex, escape the string to prevent malicious attacks:
RegExp.escape = function(string) {
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
};
new RegExp(RegExp.escape(string));
var str = "aVal,bVal,cVal,dVal,eVal";
var rgx = 'bVal';
var x = 'replacement';
var res = str.replace(rgx, x);
console.log(res);
Try this
var targetValue = 'bVal';
var replaceValue = 'yourValue';
str = str.replace(targetValue , replaceValue);

Get split number and string from string

I have an array of string like below:
var array =[];
array.push("Complex12");
array.push("NumberCar1");
array.push("Protect5");
I want to split the string and number of each item.
var Id = parseInt(array[0].match(/\d/g));
var type = array[0].replace(/\d+/g, '');
But I only get Id = 1(I want 12) and type = "Complex", where am I wrong?
thanks
I think you just missed + in first regexp
var Id = parseInt(array[0].match(/\d+/g));
Do it in one pattern with capture groups:
var mystr = "Complex12";
if (m = mystr.match(/^([a-z]+)([0-9]+)$/i)) {
var type = m[1];
var id = m[2];
}

How to get particular string from one big string ?

I have string
var str = "Ahora MXN$1,709.05" and wanted to get only
"MXN$1,709.05" from this.
Can someone please help me?
You can use substring or replace. With replace you are going to replace something with nothing.
replace
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.replace(sub,'');
substring
var str = 'Ahora MXN$1,709.05';
var sub = 'Ahora ';
var res = str.substring(sub.length);
JsFiddle
You can use either substring or Regex
Using substring
var str = "Ahora MXN$1,709.05";
var result = str.substring('Ahora '.length);
console.log(result);
Using Regex
var str = "Ahora MXN$1,709.05";
var myRegexp = /Ahora\s(.*?)(?:\s|$)/g;
var match = myRegexp.exec(str);
console.log(match[1]);

Splitting string array of one value fails

I have this:
var ID= "12,32,23,78";
var i = ID.split(',');
If I do this then it works fine, but when it is only one value like 12, then it gives me 0. How I can solve this issue? If I need to check for only one value, how do you do that?
If the variable "ID" is the number 12, then of course it doesn't work — the .split() method is a method for strings, not numbers. Try this:
var ID = /* whatever */;
var i = (ID + '').split(',');
var i;
if (ID.indexOf(",") != -1)
i = ID.split(',');
else
i = ID;
Exactly like what you posted except you check for the presence of the seperator with JavaScripts .indexOf() string method.
var ID= "12,32,23,78";
var i = ID.split(',');
will return [12,32,23,78]
var ID= "12";
var i = ID.split(',');
will return [12] -- this is also an array
however you may do this
var ID= "12";
var i = ID.split(',') || ID;
String.prototype.mySplit = function(sep) {
return (this.indexOf(sep) != -1) ? this.split(sep) : [this];
};
Example:
//var ID= '12,32,23,78';
var ID= '12';
//Update
if (typeof(ID)=='number') ID += '';
var i = ID.mySplit(',');
alert(i[0]);

Categories