This is my original string,
required:true,validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']
I want to split into two. the output will be like
required:true
validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']
Can someone help me out with this.
first solution you split the string to obtain the two values in an Array :
var str = "required:true,validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']";
var arr = str.split(",");
var result = [];
result.push(arr[0]);
result.push(arr.filter((element, index) => (index>0)).join());
console.log(result);
second solution you extract from the initial string two strings containing your values :
var str = "required:true,validType:'timegt['#timeofdaymeterslotonebegintime,#timeofdaymeterslotoneendtime']";
var index = str.indexOf(",");
var result1 = str.slice(0, index);
var result2 = str.slice(index+1);
console.log(result1);
console.log(result2);
Related
I am trying to extract some strings from a word with some pattern like -
"38384-1-page1-2222", "1-22-page33-02", "99-222-frontpage-111"
how will I extract all word between - separately, means first word before - and then second word between - and - and so on...
string = "38384-1-page1-2222";
string.substr(0, string.indexof("-")); //return 38384
But how will I extract 1, page1 and 2222 all the words separately?
The javascript function str.split(separator) split the string by the given separator and it returns an array of all the splited string. REF Here
Here is an example following your question :
var string = "38384-1-page1-2222";
var separator = "-";
var separated = string.split(separator);
var firstString = separated[0]; // will be '38384'
var secondString = separated[1]; // will be '1'
var thirdString = separated[2]; // will be 'page1'
/* And So on ... */
Hope this can help
Use String.prototype.split() to get your string into array
var words = ["38384-1-page1-2222", "1-22-page33-02", "99-222-frontpage-111"];
var resultArray = [];
for (let i = 0; i < words.length;i++) {
let temp = words[i];
resultArray = pushArray(temp.split("-"), resultArray)
}
console.log(resultArray)
function pushArray (inputArray, output) {
for (let i = 0; i < inputArray.length;i++) {
output.push(inputArray[i]);
}
return output;
}
Or simply use Array.prototype.reduce()
var words = ["38384-1-page1-2222", "1-22-page33-02", "99-222-frontpage-111"];
var result = words.reduce((previousValue, currentValue) => previousValue.concat(currentValue.split("-")), [])
console.log(result)
You can use regex /[^-]+/g
const words = ["38384-1-page1-2222", "1-22-page33-02", "99-222-frontpage-111"];
console.log(words.map(v=>v.match(/[^-]+/g)).flat())
var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.";
//Finished result should be:
result == ["10000.", "9409.", "13924.", "11025.", "10000.", "_.", "11025.", "13225.", "_.", "9801.", "12321.", "12321.", "11664."]
After each "." I want to split it and push it into an array.
You split it, and map over. With every iteration you add an . to the end
var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.";
let result = stringToSplit.split(".").map(el => el + ".");
console.log(result)
You could match the parts, instead of using split.
var string = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.",
result = string.match(/[^.]+\./g);
console.log(result);
var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.";
var arr = stringToSplit.split(".").map(item => item+".");
console.log(arr);
split the string using . delimiter and then slice to remove the last empty space. Then use map to return the required array of elements
var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.";
let newData = stringToSplit.split('.');
let val = newData.slice(0, newData.length - 1).map(item => `${item}.`)
console.log(val)
you could use a lookbehind with .split
var stringToSplit = "10000.9409.13924.11025.10000._.11025.13225._.9801.12321.12321.11664.";
let out = stringToSplit.split(/(?<=\.)/);
console.log(out)
I want to split an array that already have been split.
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var array_dt = string.split(',');
var array_s = array_dt.split('|');
console.log(array_s);
That code returns TypeError: array_dt.split is not a function.
I'm guessing that split() can not split an array. Have I wrong?
Here's how I want it to look like. For array_dt: 2016-08-08,2016-08-07,2016-08-06,2016-08-05,2016-08-04. For array_s: 63,67,64,53,63. I will use both variables to a chart (line) so I can print out the dates for the numbers. My code is just as example!
How can I accomplish this?
Demo
If you want to split on both characters, just use a regular expression
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var array_dt = string.split(/[,|]/);
console.log(array_dt)
This will give you an array with alternating values, if you wanted to split it up you can do
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var array_dt = string.split(/[,|]/);
var array1 = array_dt.filter( (x,i) => (i%2===0));
var array2 = array_dt.filter( (x,i) => (i%2!==0));
console.log(array1, array2)
Or if you want to do everything in one go, you could reduce the values to an object
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var array = string.split(/[,|]/).reduce(function(a,b,i) {
return a[i%2===0 ? 'dates' : 'numbers'].push(b), a;
}, {numbers:[], dates:[]});
console.log(array)
If performance is important, you'd revert to old-school loops, and two arrays
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var array = string.split(/[,|]/);
var array1 = [];
var array2 = [];
for (var i = array.length; i--;) {
if (i % 2 === 0) {
array1.push(array[i]);
} else {
array2.push(array[i]);
}
}
console.log(array1, array2)
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var array_dt = [];
var array_s = [];
string.split('|').forEach(function(el){
var temp = el.split(",");
array_dt.push(temp[0]);
array_s.push(temp[1]);
});
console.log(array_dt);
console.log(array_s);
Just do it one step at a time - split by pipes first, leaving you with items that look like 2016-08-08,63. Then for each one of those, split by comma, and insert the values into your two output arrays.
var string = '2016-08-08,63|2016-08-07,67|2016-08-06,64|2016-08-05,53|2016-08-04,63';
var arr = string.split("|");
var array_dt = [];
var array_s = [];
arr.forEach(function(item) {
var x = item.split(",");
array_dt.push(x[0]);
array_s.push(x[1]);
});
I am new in javascript. I want to replace string value from array if array key value match with string value
Here is my following code:
var arr= [];
arr[11] = 'XYZ';
arr[12] = 'ABC';
var string = "11-12";
My Output will be :
var str ="XYZ-ABC";
Use String#replace method with a callback.
var arr = [];
arr[11] = 'XYZ';
arr[12] = 'ABC';
var string = "11 - 12";
// match all digits in string and replace it with
// corresponding value in `arr`
var res = string.replace(/\d+/g, function(m) {
return arr[m];
})
console.log(res);
You can use regx.test() to get the Boolean value to check if it is character or not .
var arr = [];
arr[11] = 'XYZ';
arr[12] = 'ABC';
if(/[a-zA-Z\s]+/.test(arr[11])&&/[a-zA-Z\s]+/.test(arr[12])){
var str=arr[11]+ " " +arr[12];
}
You just need array methods (split map and join), neither regex nor jquery:
var str = string.split("-").map(elem => arr[elem]).join("-");
I have a string as follows :
var str = "a,b,c,a,e,f";
What I need is replace the last comma separated element by another.
ie, str = "a,b,c,a,e,anystring";
I have done it using split method and adding it to make a new string. But it is not working as expected
What I done as follows :
var str = "a,b,c,d,e,f";
var arr = str.split(',');
var res = str.replace(arr[5], "z");
alert(res);
Is there any regex to help?
You can use replace() with regex /,[^,]+$/ to match the last string
var str = "a,b,c,d,e,old";
var res = str.replace(/,[^,]+$/, ",new");
// or you can just use
// var res = str.replace(/[^,]+$/, "new");
document.write(res);
Or you can just use regex str.replace(/[^,]+$/, "new");
var str = "a,b,c,d,e,old";
var res = str.replace(/[^,]+$/, "new");
document.write(res);
Or using split() , replace the last array value with new string and then join it again using join() method
var str = "a,b,c,d,e,old";
var arr = str.split(',');
arr[arr.length - 1] = 'new';
var res = arr.join(',');
document.write(res);
You could just use a String.substring() of String.lastIndexOf():
function replaceStartingAtLastComma(str, rep){
return str.substring(0, (str.lastIndexOf(',')+1))+rep;
}
console.log(replaceStartingAtLastComma('a,b,c,d,e,f', 'Now this is f'));