1

I have a URL as string and I want to remove , (comma) from parsed URL.

I want to get 1 but it displays ,1

Here is my source:

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "http://127.0.0.1:8000/detaje/1";
    var res = str.split("http://127.0.0.1:8000/detaje/");
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>
0

4 Answers 4

1

res in your code is an array where first item is empty string, and it's converted to string. You need to access second element

document.getElementById("demo").innerHTML = res[1];
Sign up to request clarification or add additional context in comments.

Comments

1

Hey if its just a string you can use the replace method to replace all occurances of "," with "" as str=str.replace(",","");

function myFunction() {
    var str = "http://127.0.0.1:8000/detaje/1";
    var res = str=str.replace(",","");
}

Comments

0

you can use string replace,

var res = str.replace("http://127.0.0.1:8000/detaje/", "");

Comments

0

It actually displays <blank space>,1 as res contains ["","1"], what you want is second element of `res', so just use res[1].

document.getElementById("demo").innerHTML = res[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.