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.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-string-indexof-method
JavaScript String indexOf() Method - GeeksforGeeks
The indexOf() method can be used to search for longer substrings: ... // JavaScript to illustrate indexOf() method // Original string let str = 'Departed Train'; // Finding index of occurrence of 'Train' let index = str.indexOf('ed Tr'); ...
Published   January 16, 2026
🌐
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 - ... The indexOf() method is used to find the first occurrence of a specified string within another string. If the substring is found, it returns the index (position) of the first occurrence of the specified value.
🌐
Elixir Forum
elixirforum.com › t › how-to-find-a-substring-position-in-a-string-something-like-indexof-in-js › 68052
How to find a substring position in a string ? something like indexOf in js | Elixir Forum
December 13, 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. | Elixir Forum
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › javascript › pdf › string_indexof.pdf pdf
JavaScript String indexOf() Method
It can be any · integer between 0 and the length of the string. The default value is 0. Return Value · Returns the index of the found occurrence, otherwise -1 if not found. Example · Try the following example. <html> <head> <title>JavaScript String indexOf() Method</title> </head> <body> ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-find-the-index-of-specific-character-in-a-string-in-javascript
JavaScript - Index of a Character in String - GeeksforGeeks
July 23, 2025 - It works similarly to indexOf() but searches from the end of the string. JavaScript · let s = "Welcome to GeeksforGeeks"; let c = "G"; let index = s.lastIndexOf(c); console.log(index); Output · 19 · The search() method is a powerful tool used to search for a specific substring within a string.
🌐
Scaler
scaler.com › home › topics › indexof() in javascript
indexOf() in JavaScript- Scaler Topics
July 5, 2022 - The indexOf method in JavaScript is mainly used in string searching operations. The indexOf() method of the String class returns the index of the first occurrence of the substring in a string.
🌐
GitHub
gist.github.com › sashak007 › d19a92207526f72e8cec53bb9c266ffa
JavaScript Implementation of indexOf string · GitHub
Taken from https://stackoverflow.com/questions/46731372/where-should-i-find-how-the-indexof-function-is-written-for-javascript-core-look
🌐
Educative
educative.io › answers › indexof-method-in-javascript
indexOf() method in JavaScript
In JavaScript, the indexOf() method takes a string and a substring and returns the starting index of the first occurrence of the given substring in that string.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Working with Strings in JavaScript - How Can You Find the Position of a Substring in a String? - Curriculum Help - 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 › how-to-find-the-index-of-an-element-that-contains-the-given-substring-in-array
How to Find the Index of an Element that Contains the Given Substring in Array ? - GeeksforGeeks
August 5, 2025 - const array = ['apple', 'banana', ... present at index", index); ... Use the findIndex() with indexOf() method to check if the substring exists in any of the element contained by the array....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › lastIndexOf
String.prototype.lastIndexOf() - JavaScript | MDN
The method returns the index of the last occurrence of the specified substring at a position less than or equal to position, which defaults to Infinity. If position is greater than the length of the calling string, the method searches the entire string.
🌐
W3Schools
w3schools.com › JAVA › ref_string_indexof.asp
Java String indexOf() Method
charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods ·
🌐
Web Reference
webreference.com › javascript › basics › string-methods
JavaScript String Methods Explained
The .indexOf() and .lastIndexOf() methods search for a specified string within another string, and return the index of the first occurrence.