How to remove portion of a string after certain character in JavaScript ? Last Updated : 15 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Given a URL and the task is to remove a portion of URL after a certain character using JavaScript. split() method: This method is used to split a string into an array of substrings, and returns the new array. Syntax:string.split(separator, limit)Parameters:separator: It is optional parameter. It specifies the character, or the regular expression, to use for splitting the string. If not used, the whole string will be returned (an array with only one item).limit: It is optional parameter. It specifies the integer that specifies the number of splits, items beyond the split limit will be excluded from the array.JavaScript String substring() Method: This method gets the characters from a string, between two defined indices, and returns the new sub string. This method gets the characters in a string between "start" and "end", excluding "end" itself. Syntax:string.substring(start, end)Parameters:start: It is required parameter. It specifies the position from where to start the extraction. Index of first character starts from 0.end: It is optional parameter. It specifies the position (excluding) where to stop the extraction. If not used, it extracts the whole string. Example 1: This example uses the substring() method to remove the portion of the string after certain character (?). html <!DOCTYPE HTML> <html> <head> <title> Remove portion of string after certain characters </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_click()"> click to remove </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var s = '/path/action?id=11612&value=44944'; el_up.innerHTML = s; function GFG_click() { s = s.substring(0, s.indexOf('?')); el_down.innerHTML = "String = "+s; } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Example 2: This example uses the split() method to remove the portion of the string after certain character (?). html <!DOCTYPE HTML> <html> <head> <title> Remove portion of string after certain character </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "GFG_click()"> click to remove </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var s = '/path/action?id=11612&value=44944'; el_up.innerHTML = s; function GFG_click() { s = s.split('?')[0] el_down.innerHTML = "String = " + s; } </script> </body> </html> Output: Before clicking on the button: After clicking on the button: Comment More infoAdvertise with us Next Article How to remove portion of a string after certain character in JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to replace a portion of strings with another value in JavaScript ? In JavaScript, replacing a portion of strings with another value involves using the `replace()` method or regular expressions. This process is essential for text manipulation tasks like formatting, sanitization, or dynamic content generation in web development and data processing applications. We ca 3 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read How to remove all Non-ASCII characters from the string using JavaScript ? In this article, we are given a string containing some non-ASCII characters and the task is to remove all non-ASCII characters from the given string. Approaches to remove all Non-ASCII Characters from String: Table of Content Using ASCII values in JavaScript regExUsing Unicode in JavaScript regExUsi 3 min read JavaScript - How to Remove All Line Breaks From a String? Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string. It is fed into replace function along with a string to replace with, which in our case is 3 min read How to Remove Backslash from JSON String in JavaScript? Removing backslash from JSON String is important because it ensures proper JSON formatting for data interchange and prevents syntax errors during parsing, leading to accurate data representation and processing in JavaScript applications. we will going to learn three different approaches to removing 2 min read Like