Delete all the following text to a character X

2

I have a query. I need to remove all the text that follows a sign that I determine , in this case the bar . I get the string of characters from a text field and store it in a variable.

Example:

www.mipagina.com/publicaciones/favoritos

I need to delete everything that follows the first bar, leaving only:

www.mipagina.com /

I do not see it necessary to place the code that I have because it has nothing of what I consult here, but to prevent something I put it.

$('#btn').click(function() {
    var url = document.getElementById('url').value;

});

I have a method in mind, and it is that somehow the chain is traversed until the first bar is detected and it continues to travel until the end, and that from the point where the first bar was detected until the end of the chain it was selected from somehow and erase, but I really have no idea how I could do this. I was consulted similar questions that have done by here but either are things of Java or are something like Delete text in quotes or Delete specific text of a string strong>. Nothing to help me then. I await your response.

    
asked by Jalkhov 18.02.2018 в 22:03
source

2 answers

3

You can use the method split () for strings:

var url = "www.mipagina.com/publicaciones/favoritos";

var frg = url.split("/");

var result = frg[0];

console.log(result)
    
answered by 18.02.2018 / 22:15
source
1

You could use the function indexOf to get the position of the special character and then use the function substring to get the text up to that position + 1 (to take into account the special character).

Example:

var texto = "www.mipagina.com/publicaciones/favoritos";
var posicionCaracter = texto.indexOf("/");
console.log(texto.substring(0, posicionCaracter + 1));
    
answered by 18.02.2018 в 22:19