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.
Return value: Returns a new array, having the splitted items.
-
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 (eccluding) where to stop the extraction. If not used, it extracts the whole string.
Return value: It returns a new string containing the extracted characters.
Example 1: This example uses the substring() method to remove the portion of the string after certain character (?).
<!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 (?).
<!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:
Recommended Posts:
- How to remove portion of a string after a certain character in PHP?
- How to get decimal portion of a number using JavaScript ?
- How to remove all classes that begin with a certain string in JavaScript?
- How to select ID that starts with certain character using jQuery ?
- How to change state continuously after a certain amount of time in React?
- How to check if URL contain certain string using PHP?
- How to insert new row at a certain index in a table in jQuery ?
- How to check an element has certain CSS style using jQuery ?
- How to select a div with a certain class using jQuery ?
- How to remove a character from string in JavaScript ?
- Insert a character after every n characters in JavaScript
- How to Remove Special Character from String in PHP ?
- How to get character array from string in JavaScript?
- How to get the last character of a string in JavaScript?
- Delete first character of a string in JavaScript
- How to remove all line breaks from a string using JavaScript?
- How to remove text from a string in JavaScript?
- How to remove spaces from a string using JavaScript ?
- How to remove all Non-ASCII characters from the string using JavaScript ?
- How to get value of a string after last slash in JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


