Perform a split with javascript

1

I'm doing a method in javascript which given an absolute url makes it relative. For this I am trying to make a split so that the url given the cut by \ I searched the internet and I am not able to find anything that works. How could I do it?

I have tried with the following, but it does not work and if I put only one \ it generates an error:

path.split("\");
var ultimo=path.lenght;
$('#url-archivo').val(path[ultimo-1]);
    
asked by adamista 29.05.2016 в 19:16
source

2 answers

3

I hope it works, it may not work for you because you are writing the length wrong.

var array = path.split("\");
var ultimo = array.length;
$('#url-archivo').val(array[ultimo-1]);
    
answered by 29.05.2016 / 19:51
source
0

In javascript the backslash \ is used to escape control characters (that is, to be able to put control characters inside a string, for example "\t" is a tabulation and "\n" is a new line character Javascript whenever you see a backslash you understand that you are giving a special meaning to the next character.

So how do you put a backslash inside a string? Using two inverted bars "\" .

That's why it does not work for you. With two it is good (two means one).

    
answered by 29.05.2016 в 19:29