How I would do this:
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
Answer from artlung on Stack OverflowHow I would do this:
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
A solution I prefer would be:
const str = 'sometext-20202';
const slug = str.split('-').pop();
Where slug would be your result
How to get substring after specific substring in javascript
How to get substring after last specific character in JavaScript? - Stack Overflow
javascript - How to return part of string before a certain character? - Stack Overflow
How can I get the return type of substring() to match a string literal?
Assuming that your string is formatted with newlines between each question and answer pair:
1. Split the entire string by each newline:
var questionAnswerStringPairs = string.split('\n');
This will create an array of questions and answer strings. Each answer will be separated by an '*'.
2. Map over the resulting array, and create matching objects:
// This will store our data.
var questionAnswerPairs = {};
// Let's go through each string from step one and add the question and
// its answer to our object.
questionAnswerStringPairs.forEach( function(pair) {
// split the string at the *
pair = pair.split('*');
// pair is now an array, the first element is the question string
// the second element is the answer. Let's set those!
var question = pair[0];
var answer = pair[1];
// now let's store that info in the object.
questionAnswerPairs[question] = answer;
});
Now, you can do this:
questionAnswerPairs['question i want the answer to'], or this: questionAnswerPairs[stringObjectWithQuestion] and you will get back your answer!
First split the questions, then filter them and finally get the answer
var questions="This was the first 3-D film*Bwana Devil\nThis was the first cartoon talking picture*Steamboat Willie\nThis was the sequel to \"Star Wars\"*The Empire Strikes Back"
var str="This was the first cartoon talking picture"
var answers = questions
.split("\n")
.filter(question => question.indexOf(str) > -1)
.map(question => question.substring(question.indexOf(str)+str.length+1))
alert(answers.join(","))
You can use String.slice with String.lastIndexOf:
var str = 'test/category/1';
str.slice(0, str.lastIndexOf('/') + 1);
// => "test/category/"
str.slice(str.lastIndexOf('/') + 1);
// => 1
The actual code will depend on whether you need the full prefix or the last slash. For the last slash only, see Pedro's answer. For the full prefix (and a variable PREFIX):
var PREFIX = "test/category/";
str.substr(str.lastIndexOf(PREFIX) + PREFIX.length);
You fiddle already does the job ... maybe you try to get the string before the double colon? (you really should edit your question) Then the code would go like this:
str.substring(0, str.indexOf(":"));
Where 'str' represents the variable with your string inside.
Click here for JSFiddle Example
Javascript
var input_string = document.getElementById('my-input').innerText;
var output_element = document.getElementById('my-output');
var left_text = input_string.substring(0, input_string.indexOf(":"));
output_element.innerText = left_text;
Html
<p>
<h5>Input:</h5>
<strong id="my-input">Left Text:Right Text</strong>
<h5>Output:</h5>
<strong id="my-output">XXX</strong>
</p>
CSS
body { font-family: Calibri, sans-serif; color:#555; }
h5 { margin-bottom: 0.8em; }
strong {
width:90%;
padding: 0.5em 1em;
background-color: cyan;
}
#my-output { background-color: gold; }
Another method could be to split the string by ":" and then pop off the end.
var newString = string.split(":").pop();