This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How might I extract the number from a number + unit of measure string using JavaScript?
How to extract number from string like this in JS.
String: "Some_text_123_text" -> 123
JSFiddle Demo
var s = "Some_text_123_text";
var index = s.match(/\d+/);
document.writeln(index);
try this
var string = "Some_text_123_text";
var find = string.split("_");
for(var i = 0; i < find.length ; i ++){
if(!isNaN(Number(find[i]))){
var num = find[i];
}
}
alert(num);
try this working fiddle
var str = "Some_text_123_text";
var patt1 = /[0-9]/g;
var arr= str.match(patt1);
var myval = arr.join("");
Related
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I have following string in javascript.
var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
I Want to replace this string into following string using jquery replace.
var str = "$('#p24').val() + $('#p33').val()";
var str = 'P24 + P33'; //p24 is just exp. it will be any number i.e. P98
var str_array = str.split(" + ");
console.log("Original string: "+str);
for(var i = 0; i < str_array.length; i++){
str_array[i] = $("#"+str_array[i]).html();
}
var replaced_string = str_array.join(" + ");
console.log("Replaced string: "+replaced_string);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="P24">this is P24</div>
<div id="P33">this is P33</div>
This question already has answers here:
Javascript split, push and join
(2 answers)
Closed 6 years ago.
I get a string of data back that is seperata by semi colons. Here is an example:
apple;orange;lemon).
I am trying to strip out the semi colons and turn the string into an array, so I can access each item individually. Then, I am trying to join them back together and print them out on the screen separated by a "/". The problem is that it is not working.
var planArray = associatedAction.split(";")
for(var i=0; i < planArray.length; i++) {
var seperatedActionPlan = planArray[i];
}
Also, I would like to put the final output into a variable, so I can print out just that variable on the page.
Please help!
EDIT!
One thing I forgot to mentioned, is that when the string prints out, I want the values to be separated. So as an example I want the final print out to be Apple/Orange/Lemon
I think this is what you are looking for:
<script type="text/javascript">
var associatedAction = "a;b;c;d";
var planArray = associatedAction.split(";")
var seperatedActionPlan = '';
for (var i = 0; i < planArray.length; i++) {
if (i < planArray.length - 1) {
seperatedActionPlan =
seperatedActionPlan.concat(planArray[i] + "/");
}
else {
seperatedActionPlan = seperatedActionPlan.concat(planArray[i]);
}
}
alert(seperatedActionPlan);
</script>
this should work
var seperatedActionPlan = planArray.join("/")
let seperatedActionPlan = associatedAction.split(";").join("/"));
Should do it.
Change your code to this:
var planArray = associatedAction.split(";"),
seperatedActionPlan;
for(var i=0; i < planArray.length; i++) {
// ... do what you need
}
seperatedActionPlan = planArray.join('/');
// print out seperatedActionPlan
or if you don't do anything to the array except splitting and joining with / just use
var seperatedActionPlan = associatedAction.replace(';', '/');
// print out seperatedActionPlan
This help you :
<html>
<head>
</head>
<body>
<script>
var associatedAction = "apple;orange;lemon";
var planArray = associatedAction.split(";")
for(var i = 0; i < planArray.length; i++ ) {
var a = document.createElement('a');
var txt = document.createTextNode("/");
a.href = "#";
a.innerHTML = planArray[i];
document.body.appendChild(a);
if (i != planArray.length - 1)
a.insertAdjacentHTML('afterend',"/");
}
</script>
</body>
</html>
This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
Closed 8 years ago.
I want a pattern like this:- GT-000001. This pattern gets incremented when a new record is inserted.
So I get values from my DB like this:
var pattern = 'GT-';
var init = 00000;
var recordnumber = 1; // This value i get dynamically.
var result = pattern + init + recodnumber;
But I get result = GT-01. I want result to be GT-000001. How to get this result?
The below example works for recordnumber upto 6 digits. outputing 'GT-000001', 'GT-000012', or 'GT-123456' based on the value of recordnumber
var pattern = 'GT-';
var recordnumber = 1; // This value i get dynamically.
var result = pattern + ('00000' + recordnumber).slice(-6);
console.log(result);
The reason you get that result is that you have
var init = 00000;
Note that the zeroes are not in quotes. That is effectively the same as:
var init = 0;
and so when you put it in the string, you get just the one zero.
If you want five zeroes, you need to use a string:
var init = "00000";
If you're trying to zero-pad, in general, this question and its answers that Matt found may be helpful.
But the short version:
var pattern = 'GT-';
var init = "000000"; // Note there are six of these, not five
var recordnumber = 1; // This value i get dynamically.
var result = String(recordnumber);
result = pattern + init.substring(result.length) + result;
Your init is number type, it is already truncated to 0 on assignment.
You need to add leading zeros manually:
function leadzeros(n, size) {
var s = n+"";
while (s.length < size) s = "0" + s;
return s;
}
var pattern = 'GT';
//var init = 00000; // <- here is 'init' is 0 already, so you can drop it
var recordnumber = 1; // This value i get dynamically.
var result = pattern + leadzeros(recodnumber, 5);
This question already has answers here:
How can I convert a comma-separated string to an array?
(19 answers)
Closed 9 years ago.
I need to know how i can use javascript to separate a string like 22,44,85,63,12 to individual numbers without the commas e.g.:
22
44
85
63
12
var a = "one,two,three".split(",") // Delimiter is a string
for (var i = 0; i < a.length; i++)
{
alert(a[i])
}
You need the .split() method like this:
var str = "22,44,85,63,12";
var res = str.split(",");
res will then be a array of your numbers.
Here is a Fiddle
Use the split and join methods
var csv = '22,44,85,63,12';
var ssv = csv.split(',').join(' ');
First split the strings -
var str = ' 22,44,85,63,12';
var arr = str.split(',');
Then create an array of numbers. Check if the element is a number first though.
var numberArr = new Array();
var number;
for(var i = 0; i < arr.length; ++i)
{
number = parseInt(arr[i], 10);
if(!isNaN(number ))
{
numberArray.push(number);
}
}
Try -
var commaSepStr = "22,44,85,63,12";
var spaceSepStr = commaSepStr.replace(/,/g,' ');
This does a global replace. From what i understood, you want the output to be a string and not an array.
Use .split() for extracting the array of number-strings, and .map() for converting those number-strings to Number:
var str = "22,44,85,63,12";
var numbers = str.split(",").map(Number); //[22,44,85,63,12]
If you just want to replace the commas with a blank space how about using
string.replace(/,/g,' ');
but if you want them as separate integers then use var nums = string.split (",");
Can anybody tell me how can i get a numeric value from a string containing integer value and characters?
For example,I want to get 45 from
var str="adsd45";
If your string is ugly like "adsdsd45" you can use regex.
var s = 'adsdsd45';
var result = s.match(/([0-9]+)/g);
['45'] // the result, or empty array if not found
You can use regular expression.
var regexp = /\d+/;
var str = "this is string and 989898";
alert (str.match(regexp));
Try this out,
var xText = "asdasd213123asd";
var xArray = xText.split("");
var xResult ="";
for(var i=0;i< xArray.length - 1; i++)
{
if(! isNan(xArray[i])) { xResult += xArray[i]; }
}
alert(+xResult);
var str = "4039";
var num = parseInt(str, 10);
//or:
var num2 = Number(str);
//or: (when string is empty or haven't any digits return 0 instead NaN)
var num3 = ~~str;
var strWithChars = "abc123def";
var num4 = Number(strWithChars.replace(/[^0-9]/,''));