Sangamesh Davey Sangamesh Davey - 1 year ago 87
Javascript Question

JavaScript split() function not working

I have a piece of string


I want the output to be:


What I have tried till now:


<!DOCTYPE html>
<p id="demo"></p>
<button type="button" onclick="myFunction()">Try</button>


function myFunction() {
var str = "res1";
var result = str.split("res");
document.write(result);//returns ,1
var mystring = result.split(',').join("");

document.getElementById("demo").innerHTML = mystring;

The error which I receive is:

Uncaught TypeError: result.split is not a function

What am I missing?

Answer Source

If you generally want to get the last character of the string you could use something like:

var str = "res1";
str.substr(str.length - 1)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download