This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 2 years ago.
I get one string by query like '5e6,5e4,123'.
And I want to make an array containing this query as below in JS.
['5e6', '5e4', '123']
How can I make this? Thank you so much for reading it.
You can use .split(',')
var str = "5e6,5e4,123";
var array = str.split(',');
console.log(array);
You can read more on this here
Use String.split:
console.log('5e6,5e4,123'.split(","))
var query = '5e6,5e4,123';
var queries = query.split(‘,’);
You can make use of split method of string like below:
var res = str.split(',');
const output = input.split(',');
Related
This question already has answers here:
Getting parts of a URL with JavaScript
(5 answers)
Closed 5 years ago.
url:
http://xxxxxx.com/video/view/12345
Can I take 12345 in the url using javascript?
Please help me
Use RegExp, Array#match and negative lookahead.
var str = 'http://xxxxxx.com/video/view/12345';
console.log(str.match(/(?!view\/)\d+/)[0]);
You can also try this if you're sure that it'll always be in last:
var num = location.pathname.split('/').pop(); // "12345"
and further: parseInt(num);
You can parse your URL with the following code. Then just get the last part.
var url = 'http://xxxxxx.com/video/view/12345';
var url_parts = url.replace(/\/\s*$/,'').split('/');
console.log(url_parts[url_parts.length - 1]); // last part
This question already has answers here:
Javascript Equivalent to PHP Explode()
(17 answers)
Closed 6 years ago.
I have this text as my post tags:
car,phone,apple,node,php
and I wanna convert that into an array, like this:
["car","phone","apple","node","php"]
and then save that into my mongodb database.
how can I do that in my server.js code?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
passing , as an argument to split, will do it for you.
> x = "car,phone,apple,node,php"
'car,phone,apple,node,php'
> x.split(",")
[ 'car', 'phone', 'apple', 'node', 'php' ]
"car,phone,apple,node,php".split(',');
will do the thing
Use javascript default split function.
var stringVar = "car,phone,apple,node,php";
console.log(stringVar.split(','));
var str ="car,phone,apple,node,php";
var arr =str.split(',');
console.log(arr);
split() function converts the string into array.
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 27 days ago.
Hi i am working in java script i have a string
var data = 'http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';
i want to replace /image/ into 'image/440x600' i am using this function
.replace()
but its not working here is my code
var data ='http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';
data.replace('/image/', '/image/440x600/');
console.log(data);
its showing same not replacing /image/ into 'image/440x600'.
Strings in JavaScript are immutable. They cannot be modified.
The replace method returns the modified string, it doesn't modify the original in place.
You need to capture its return value.
var data = 'http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';
data = data.replace('/image/', '/image/440x600/');
console.log(data);
Strings in JavaScript are immutable. Thus the replace function doesn't change the string but returns a new one, you have to use the returned value:
var data = data.replace('/image/', '/image/440x600/');
//Your Actual Data
var data ='http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';
// Changing the reference of the Actual Data and gets a new String
var ChangedData =data.replace('/image/', '/image/440x600/');
// To Verify the Output
console.log(data);
console.log(ChangedData);
Please check this
var str = "http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg";
var res = str.replace("image", "image/440x600");
console.log(res);
Using global regular expression
var data = data.replace(/image/g, '/image/440x600/');
This question already has answers here:
How to convert an integer to an array in PHP?
(4 answers)
Closed 8 years ago.
I have 23681 its not string but integer , i want to make an array by placing each number on an index in php or javascript any guide would be appreciated
[0]=>2
[1]=>3
[2]=>6
[3]=>8
[4]=>1
Javascript
result = (23681).toString().split("").map(Number);
You can use str_split():
$array = str_split($yourNumber);
Your int will be casted to string automatically. So no implicit casting needed here.
Another javaScript solution without string conversion
var number = 12345, result=[];
while(number>0) {
result.push(number % 10);
number = Math.floor(a / 10);
}
result.reverse()
quick and easy:
$your_number_arr = array_map('intval', str_split($your_number));
edit: integer conversion
You should be more specific. I don't know if you need this in Javascript or PHP, but here it is in php:
$str = ((string)$int);
$array = str_split($str);
This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 8 years ago.
I am trying to get the first word out of the variable var solution = [cow, pig]
I have tried everything from strings to arrays and I can't get it. Please help.
As per the comments
solution[0]
Will return the first item in the array.
solution[1]
would be the second, or undefined if the array was:
var solution = [cow]
Is solution an array, or is it in that form? (var solution = [cow, pig]) You also need to add quotes around those values, unless those values are defined variables.
You need to change the variable to look like this:
var solution = ['cow', 'pig']
If so, just get the value at subscript 0.
var result = solution[0];
console.log(result);
If you mean an string like
solution = "cow pig".
Do
solution = solution.split(' ')[0];
console.log(solution); //Will return cow