0

I have this string: "gss_save,cls,x_value". I want to convert it to "x_value". I tried this:

//var cls is "gss_save,cls,x_value";
var fnl = cls.replace('gss_save,cls,','');
console.log(fnl);

As suggested by other answers in this site, but I get: Uncaught TypeError: undefined is not a function in line var fnl = cls.replace('gss_save,cls,','');. Expecting to get "x_value".

2
  • That works fine for me. Can you show more code? jsfiddle.net/BloodyKnuckles/n9fjhLaj Commented Apr 7, 2015 at 23:33
  • Sorry it was an array I used toString and it worked, thank you anyway Commented Apr 7, 2015 at 23:35

2 Answers 2

1

Another way is

var fnl = cls.split(',');
fnl= fnl[fnl.length-1]
console.log(fnl);

Sign up to request clarification or add additional context in comments.

Comments

0

You need a 'regex pattern' which looks like /.../ without quotation marks!

string.replace(pattern, replacement)

Example:

var str = "gss_save,cls,x_value";
var pattern = /gss_save,cls,/;
var res = str.replace(pattern, "");

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.