grab the string between characters with jquery

advertisements

I have a variable which contains "j_id0:j_id11:i:f:pb:d:MyFieldName.input" (without the quotes).

Now I would like to capture "MyFieldName".

I had this:

var test = "j_id0:j_id11:i:f:pb:d:MyFieldName.input";
var testRE = test.match(":(.*).input");
console.log("old text: " + test);
console.log("new text: " + testRE[1]);

which outputs this:

old text: j_id0:j_id11:i:f:pb:d:MyFieldName.input
new text: j_id11:i:f:pb:d:MyFieldName

So what I need is telling him that I want everything between the last occurence ":" and ".input", because now he finds the first ':' and stops there.

Any ideas how I can realise this?

Thanks!


One option that remains (among, presumably, many others) is:

var str = "j_id0:j_id11:i:f:pb:d:MyFieldName.input",
    fieldName = str.substring(str.lastIndexOf(':') + 1, str.lastIndexOf('.'));

JS Fiddle demo.

References: