0

I need to extract the id pKmuXgem from the following function (being returned as a string):

"someFunction('Short Description','','pKmuXgem'); return false;"

The id may change, but the surrounding markup should stay the same. I can do a simple string replace:

var str = "someFunction('Short Description','','pKmuXgem'); return false;";
var id = str.replace("someFunction('Short Description','','", "");
id = str.replace("'); return false;", "");

but that feels fragile.

What is best practice?

bcerasani
  • 3
  • 2
  • [JavaScript parser in JavaScript](http://stackoverflow.com/questions/2554519/javascript-parser-in-javascript) would be bullet-proof solution... – Alexei Levenkov Oct 25 '14 at 02:42

1 Answers1

0

You can use RegEx.

Demo

var data = "someFunction('Short Description','','pKmuXgem'); return false;";
var match = /,'([^']+?)'\)/g.exec(data);
console.log(match[1]);
Weafs.py
  • 22,731
  • 9
  • 56
  • 78
  • 1
    This assumes there are only `[a-zA-Z]` in the string. Why? It could be anything except a single quote. – jfriend00 Oct 25 '14 at 02:55