I have my url like https://app.asana.com/0/1154029233771298/1161783458298546; I need to get result like 1154029233771298/1161783458298546. Thanks in forward!
You can split the URL string by the / character and rejoin the last two elements by the same character:
function extractLastWords(url) {
return url.split('/').slice(-2).join('/')
}
var u = 'https://app.asana.com/0/1154029233771298/1161783458298546'
extractLastWords(u) // => "1154029233771298/1161783458298546"
var arr = str.split("/"); // create array of strings that are separated by '/'
var result = arr[arr.length-2] + "/" + arr[arr.length-1]; // concatenate last 2 indices
Or alternatively:
var result = str.split("/").slice(-2).join("/");
.split("/") - create array of strings from delimiter
.slice(-2) - get sub-array with only the last 2 elements
join("/") - concatenate all strings in array with delimiter
How can I output an array as a scentence except the (1) item? Let's say the content of the array is: ["!report","Jay","This","is","the","reason"];
I tried this to output the items after the (1): (args.slice(1));however the output now is: "This,is,the,reason", how could I make it output as a normal scentence?
If you don't want to use built in methods, you can append each word
in the array starting at index 1 (second item).
// List of words
var words = ["!report","Jay","This","is","the","reason"];
// Empty string
var sentence = "";
// Loop through array starting at index 1 (second item)
for (let i = 1; i < words.length; i++) {
// Keep appending the words to sentence string
sentence = sentence + words[i] + " ";
}
// Print the sentence as a whole
console.log(sentence);
Or using built in functions:
// Array of strings
var array = ["!report","Jay","This","is","the","reason"];
// Cut off the first element, words is still an array though
var words = array.slice(1)
// Join each element into a string with spaces in between
var sentence = words.join(" ")
// Print as full sentence
console.log(sentence)
Output:
"Jay This is the reason"
You could slice from the second element and join the array.
console.log(["!report","Jay","This","is","the","reason"].slice(2).join(' '));
.slice() returns a new array, so when you access it as a whole, you often see a comma separated list of the array values.
But, .slice() along with .join() does the trick. .join() allows you to "join" all the array values as a single string. If you pass an argument to .join(), that argument will be used as a separator.
You can then just concatenate a period (.) to the end of the string.
console.log(["!report","Jay","This","is","the","reason"].slice(1).join(" ") + ".");
The output you desire is not very clear (do you want to remove only the first item or also the second). However the methods are the same:
you can use destructuring assignment syntax if you're es6 compliant
const arr = [a,b,...c] = ["!report","Jay","This","is","the","reason"];
let sentence = c.join(" ");
// or
let sentence2 = c.toString().replace(/,/g," ");
console.log (sentence," - ",sentence2);
or simply replace with regex and a correct pattern
const arr = ["!report","Jay","This","is","the","reason"];
let sentence = arr.toString().replace(/^[A-z! ]+?,[A-z ]+?,/,"").replace(/,/g," ");
// or
let sentence2 = arr.toString().replace(/^[A-z! ]+?,/,"").replace(/,/g," ");
console.log (sentence," - ",sentence2);
Here it is, check fiddle comments for code explanation.
var a = ["!report","Jay","This","is","the","reason"];
//removes first element from array and implodes array with spaces
var sentence = a.slice(1).join(" ");
console.log(sentence);
I have a String such as ABC_DEF_GHI.
Using JavaScript I need to get everything after the first _ (that is, DEF_GHI in this case). There could be any number of _ in the String.
If I do something like
var str = "ABC_DEF_GHI_JKL";
var n = str.lastIndexOf('_');
var output = str.substring(n + 1);
This would give me everything after the last underscore. However, I need everything after the first underscore. Couldn't find a method such as firstIndexOf which would give me everything after the first _
You should replace your lastIndexOf() by indexOf() which will take the first occurrence
var str = "ABC_DEF_GHI_JKL";
var n = str.indexOf('_');
var output = str.substring(n + 1);
console.log(output);
var str = "ABC_DEF_GHI",
pos = str.indexOf("_");
result = str.slice(pos+1);
console.log(result);
console.log("ABC_DEF_GHI_JKL".split('_').slice(1).join('_'));
The above simply split's your string into an array, splitting at the _, and then drops the first array element, and then joins them back together with _.
One way of doing this is
var str = "ABC_DEF_GHI",
pos = str.indexOf("_");
result = str.slice(pos+1);
console.log(result);
How can I cut text from the end of a variable up until a specific character?
Like this:
a_a_a
And I want the last "A" and split the text at the last "_":
a_a_ | a
Then I want to get 2 strings witch would be like this:
string A = a_a_
string B = a
You can use lastIndexOf to get the last occurance of the character to be searched and then substr to get the string from that index
var str = "a_a_a";
var startIndex = str.lastIndexOf("_"); //Here we are getting the last index of _ char
var result = str.substr(startIndex); //this will output `_a`
var result = str.substr(startIndex+1); //as we need only `a` we are using `startIndex + 1`
One way;
var a = s.substr(0, s.lastIndexOf("_") + 1);
var b = s.substr(a.length);
I'm trying to split a string into an array based on the second occurrence of the symbol _
var string = "this_is_my_string";
I want to split the string after the second underscore. The string is not always the same but it always has 2 or more underscores in it. I always need it split on the second underscore.
In the example string above I would need it to be split like this.
var split = [this_is, _my_string];
var string = "this_is_my_string";
var firstUnderscore = string.indexOf('_');
var secondUnderscore = string.indexOf('_', firstUnderscore + 1);
var split = [string.substring(0, secondUnderscore),
string.substring(secondUnderscore)];
Paste it into your browser's console to try it out. No need for a jsFiddle.
var string = "this_is_my_string";
var splitChar = string.indexOf('_', string.indexOf('_') + 1);
var result = [string.substring(0, splitChar),
string.substring(splitChar, string.length)];
This should work.
var str = "this_is_my_string";
var matches = str.match(/(.*?_.*?)(_.*)/); // MAGIC HAPPENS HERE
var firstPart = matches[1]; // this_is
var secondPart = matches[2]; // _my_string
This uses regular expressions to find the first two underscores, and captures the part up to it and the part after it. The first subexpression, (.*?_.*?), says "any number of characters, an underscore, and again any number of characters, keeping the number of characters matched as small as possible, and capture it". The second one, (_.*) means "match an underscore, then any number of characters, as much of them as possible, and capture it". The result of the match function is an array starting with the full matched region, followed by the two captured groups.
I know this post is quite old... but couldn't help but notice that no one provided a working solution. Here's one that works:
String str = "this_is_my_string";
String undScore1 = str.split("_")[0];
String undScore2 = str.split("_")[1];
String bothUndScores = undScore1 + "_" + undScore2 + "_";
String allElse = str.split(bothUndScores)[1];
System.out.println(allElse);
This is assuming you know there will always be at least 2 underscores - "allElse" returns everything after the second occurrence.