Use a debugger to find out what you are getting in the userInput. The code is fine. It should work. See sample code below.

test = function() {
    var test = "http://Test 2"
    alert(test.substring(0, test.indexOf("://")))
}
Answer from user883986 on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › indexOf
String.prototype.indexOf() - JavaScript | MDN
The indexOf() method of String values searches this string and returns the index of the first occurrence of the specified substring. It takes an optional starting position and returns the first occurrence of the specified substring at an index greater than or equal to the specified number.
🌐
W3Schools
w3schools.com › jsref › jsref_indexOf.asp
JavaScript String indexOf() Method
The indexOf() method returns the position of the first occurrence of a value in a string.
Discussions

Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String?
Tell us what’s happening: Am I missing something, or is there an issue with the following concept check question? The official correct answer is (C), but isn’t (B) correct as well? According to MDN: String.prototyp… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
January 9, 2025
How to find a substring position in a string ? something like indexOf in js
I have read the docs of String module, it seems that there’s nothing like IndexOf in js which return index of a substring in string. More on elixirforum.com
🌐 elixirforum.com
0
December 12, 2024
Do you know? The indexOf() function can take a second parameter.
I never had to use for loop in javascript More on reddit.com
🌐 r/learnjavascript
30
53
May 18, 2020
Find the Index of the First Occurrence in a String
The loop is for the starting character index to check. And an indexed string expression: str[i] will only return one character. Not the string which starts from the given character index. Use the string's slice() method instead. Giving it the starting character index, and the exact same length as the check string. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice More on reddit.com
🌐 r/learnjavascript
7
3
October 5, 2023
🌐
Mimo
mimo.org › glossary › javascript › string-indexof-method
JavaScript String indexOf() method: Syntax, Usage, and Examples
The indexOf() method in JavaScript allows you to find the position of a substring within a string. It returns the index of the first occurrence or -1 if the substring doesn’t exist.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String? - JavaScript - The freeCodeCamp Forum
January 9, 2025 - Tell us what’s happening: Am I missing something, or is there an issue with the following concept check question? The official correct answer is (C), but isn’t (B) correct as well? According to MDN: String.prototype.indexOf() Array.prototype.indexOf() Your browser information: User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:134.0) Gecko/20100101 Firefox/134.0 Challenge Information: Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String?
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-indexof-method
JavaScript String indexOf() Method - GeeksforGeeks
Here’s a simple example that finds the position of a substring within a string: ... // Original string let str = 'Departed Train'; // Finding index of occurrence of 'Train' let index = str.indexOf('Train'); console.log(index);
Published   January 16, 2026
Find elsewhere
🌐
Elixir Forum
elixirforum.com › questions / help
How to find a substring position in a string ? something like indexOf in js - Questions / Help - Elixir Programming Language Forum
December 12, 2024 - I have read the docs of String module, it seems that there’s nothing like IndexOf in js which return index of a substring in string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
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.
🌐
Tabnine
tabnine.com › home › how to use the indexof method in javascript
How to Use the indexOf method in JavaScript - Tabnine
July 25, 2024 - The indexOf() method is a part ... of both types. The method returns the first index at which a given element can be found in the Array or String, or -1 if the element being searched for was not found. Tip: To […...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › indexOf
JavaScript String indexOf() - Find Character Position
November 13, 2024 - The indexOf() method in JavaScript is a critical tool for finding the position of a substring within a string. This method returns the index of the first occurrence of the specified value, or -1 if the value isn't found.
🌐
Oreate AI
oreateai.com › blog › mastering-javascripts-indexof-your-guide-to-string-searching › a5053f02d3f3230485133e74bd3515fe
Mastering JavaScript's indexOf: Your Guide to String Searching - Oreate AI Blog
January 21, 2026 - The syntax is straightforward: string.indexOf(searchValue[, fromIndex]). Here, searchValue is mandatory; it represents the substring you're looking for.
🌐
Medium
medium.com › @anandgupta20588 › indexof-vs-includes-method-to-search-a-string-character-f48cdf1261ee
indexOf() vs includes() method to search a string/character | by Anandgupta | Medium
November 2, 2023 - Use indexOf() when you need to find the position of a substring within a string, and use includes() when you only need to know if a substring exists within a string without the need for its position.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-split-and-manipulate-strings-in-javascript
How To Index, Split, and Manipulate Strings in JavaScript | DigitalOcean
August 24, 2021 - To summarize, charAt() and slice() will help return string values based on index numbers, and indexOf() and lastIndexOf() will do the opposite, returning index numbers based on the provided string characters.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › indexOf
Array.prototype.indexOf() - JavaScript | MDN
The indexOf() method compares searchElement to elements of the array using strict equality (the same algorithm used by the === operator).
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.indexof()
JavaScript String indexOf() Method
November 3, 2024 - Summary: in this tutorial, you’ll learn how to use the JavaScript String indexOf() method to find the index of the first occurrence of a substring within a string.
🌐
DEU University
web.deu.edu.tr › doc › oreily › web › jscript › refp_363.html
[Chapter 21] Reference: String.indexOf()
The search begins at position start within string, or at the beginning of string, if start is not specified. If an occurrence of substring is found, then String.indexOf() returns the position of the first character of the first occurrence of substring within string.
🌐
Bacancy Technology
bacancytechnology.com › qanda › javascript › string-contains-a-substring-in-javascript
How to Check If a String Contains a Substring in JavaScript
June 24, 2024 - In JavaScript, you can check whether ... approaches: The indexOf() method returns the index within the string of the first occurrence of the specified substring, or -1 if the substring is not found....
🌐
Sentry
sentry.io › sentry answers › javascript › how to check whether a string contains a substring in javascript?
How to Check Whether a String Contains a Substring in JavaScript? | Sentry
The default position is 0. If you want to begin searching at position 2, you would write: ... The indexOf() method is also case sensitive, and returns the index position of the first occurrence of the substring within the string.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › string › all indexes of substring
Find all indexes of a substring in a JavaScript string - 30 seconds of code
March 10, 2024 - const indexOfSubstrings = function* (str, searchValue) { let i = 0; while (true) { const r = str.indexOf(searchValue, i); if (r !== -1) { yield r; i = r + 1; } else return; } }; [...indexOfSubstrings('tiktok tok tok tik tok tik', 'tik')]; // [0, 15, 23] [...indexOfSubstrings('tutut tut tut', 'tut')]; // [0, 2, 6, 10] [...indexOfSubstrings('hello', 'hi')]; // [] ... JavaScript generator functions are an advanced yet very powerful ES6 feature, which you can start using in your code right now. ... Get all the partial substrings of a string in JavaScript using generator functions.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-indexof
Exploring the indexOf Method for Strings and Arrays in JavaScript | DigitalOcean
December 17, 2019 - An overview of how the indexOf() method works to find the position of a substring in a string or an item in an array in Javascript.