Getting string from another string in Javascript

31 Views Asked by At

Please note that this is not a json object :) My string is: {"message":"***error in SAP module:-1***","status":400} This is not a json object, this is a pure string. I cannot turn it into a json object due to technical limitations.

So, I want to take only the bold string (all the value of "message"). I thought about lastIndexOf, and pick the string between ":" and "," But I got messed up with the escape characters for the quotes. How can I achieve it with lastIndexOf? Or in another better way?

1

There are 1 best solutions below

2
ginga_ninja217 On

If you can't use JSON.parse, I would use a regex to read it. You know you want the string directly following "message":", so I would look for that, then grab everything from there to the next occurrence of "