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 Overflow
Discussions

How to get substring after specific substring in javascript
I am making a bot to automatically answer to trivia questions. I am stuck at this point. I have the string of the question and I want to estrapolate it from the string with all the question of the ... More on stackoverflow.com
🌐 stackoverflow.com
How to get substring after last specific character in JavaScript? - Stack Overflow
I have a string test/category/1. I have to get substring after test/category/. How can I do that? More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
javascript - How to return part of string before a certain character? - Stack Overflow
This is the correct answer, not the method above, to obtain everything after the colon character in a string. 2018-08-10T21:12:10.943Z+00:00 ... And note that first argument of subString is 0 based while second is one based. ... function getStringAfterSubstring(parentString, substring) { return ... More on stackoverflow.com
🌐 stackoverflow.com
December 30, 2018
How can I get the return type of substring() to match a string literal?
Here's how I'd probably do it: type FirstLetter = T extends `${infer F}${string}` ? F : never; const directions = ["ene", "wnw", "ssw"] as const type Direction = typeof directions[number] function getFirstLetter(s: T): FirstLetter { // could use a type guard instead of as FirstLetter return s.substring(0, 1) as FirstLetter; } const e = getFirstLetter("ene"); // "e" const w = getFirstLetter("wnw"); // "w" const error = getFirstLetter("abc") // "abs is not assignable to...." More on reddit.com
🌐 r/typescript
8
4
January 22, 2025
🌐
W3Schools
w3schools.com › jsref › jsref_substring.asp
JavaScript String substring() Method
cssText getPropertyPriority() ... ... More examples below. ... The substring() method extracts characters, between two indices (positions), from a string, and returns the substring....
🌐
Sabe
sabe.io › blog › javascript-get-substring-after-specific-character
Get the Substring after a Specific Character in JavaScript | Sabe
May 10, 2022 - We can use the substring() method to get the substring after the index that we just calculated. JAVASCRIPTconst input = "1) Hello World"; const index = input.indexOf(")"); const substring = input.substring(index + 1); console.log(substring);
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-get-a-part-of-string-after-a-specified-character-in-javascript
How to get a part of string after a specified character in JavaScript?
March 15, 2026 - <html> <body> <script> function getStringPart(string, character, position) { if (position === 'after') { return string.substring(string.indexOf(character) + 1); } else if (position === 'before') { return string.substring(0, string.indexOf(c...
Top answer
1 of 3
1

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!

2 of 3
1

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(","))

🌐
W3Resource
w3resource.com › javascript-exercises › javascript-string-exercise-22.php
JavaScript validation with regular expression: Get a part of string after a specified character - w3resource
July 17, 2025 - Write a JavaScript function to get a part of string after a specified character. Test Data: console.log(subStrAfterChars('w3resource: JavaScript Exercises', ':','a')); console.log(subStrAfterChars('w3resource: JavaScript Exercises', 'E','b')); Output: "w3resource" "xercises"
Find elsewhere
🌐
Dirask
dirask.com › posts › JavaScript-get-substring-after-word-D9aB2D
JavaScript - get substring after word
get substring after word in · JavaScript. In this example, we use indexOf() method to get the index of a word substring. Then we use slice() to get the substring from index + length index to the end of the text string.
🌐
ReqBin
reqbin.com › code › javascript › i5foejaa › javascript-substring-example
How do I get a substring from a string in JavaScript?
1 month ago - To get a substring from a string in JavaScript, you can use the built-in string.substring(start, end) method.
🌐
GitHub
github.com › stdlib-js › string-substring-after
GitHub - stdlib-js/string-substring-after: Return the part of a string after a specified substring. · GitHub
$ echo -n 'bar\tbaz' | substring-after --search b --split '\t' ar az · @stdlib/string-substring-before: return the part of a string before a specified substring.
Author: stdlib-js
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.substring()
JavaScript Substring(): Extract a Substring from a String
November 3, 2024 - The startIndex specifies the index of the first character to include in the returned string. The endIndex determines the first character to exclude from the returned substring. In other words, the returned substring doesn’t include the character at the endIndex. If you omit the endIndex, the substring() returns the substring to the end of the string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript - MDN Web Docs
The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › XPath › Functions › substring-after
substring-after - XPath | MDN
January 31, 2025 - The substring-after function returns a string that is the rest of a given string after a given substring.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-substring-method
JavaScript String substring() Method - GeeksforGeeks
If only the starting index is provided, substring() will return the substring from that index to the end of the string. ... Like most string methods in JavaScript, substring() does not alter the original string.
Published: January 16, 2026
🌐
Edureka
edureka.co › blog › javascript-substring
JavaScript Substring() | How is it different from substr() & slice()? | Edureka
January 9, 2025 - Manipulating data is an integral part of any web application. JavaScript provides different ways for parsing and extracting data. The JavaScript Substring, Substr and Slice methods are used for extracting strings from a larger one.
🌐
Refine
refine.dev › home › blog › tutorials › javascript substring method
JavaScript Substring Method | Refine
January 1, 2025 - While doing so, we elaborate on a few patterns of extraction from a parent string: namely, that of extracting a tail after first n characters, another of producing a substring composed of first n characters and that of extracting last n characters. In the later half of the post, we discuss some of the nuances associated with startIndex and endIndex values. In the end, we point out how JavaScript String.prototype.substring() compares to String.prototype.slice() as well as the deprecated method String.prototype.substr().
🌐
javaspring
javaspring.net › blog › get-value-of-a-string-after-last-slash-in-javascript
How to Get the Substring After the Last Slash in a JavaScript String: Easy Methods Using substr() and indexOf() — javaspring.net
The simplest and most efficient way to get the substring after the last slash is to combine lastIndexOf() (to find the position of the last slash) with substr() (to extract the substring after that position).