Sangamesh Davey Sangamesh Davey - 4 months ago 15
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?


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)