I am trying to retrieve the data to the right of ':' or use ':' as a starting point to get the data after. Looking for the right parsing items for this.
var test = 'type:input';
charAt gives me the location of ':', but how do I then retrieve the 'input' from var test?
I am trying to retrieve the data to the right of ':' or use ':' as a starting point to get the data after. Looking for the right parsing items for this.
var test = 'type:input';
charAt gives me the location of ':', but how do I then retrieve the 'input' from var test?
Share Improve this question edited Nov 6, 2011 at 13:19 Šime Vidas 186k65 gold badges289 silver badges391 bronze badges asked Nov 6, 2011 at 13:04 user1009749user1009749 633 silver badges8 bronze badges2 Answers
Reset to default 11You can use split
:
var testAfter = test.split(':')[1];
or substr
with indexOf
var testAfter = test.substr(test.indexOf(':')+1);
or slice
var testAfter = test.slice(test.indexOf(':')+1);
or match
var testAfter = test.match(/(:)(.+$)/).pop();
or replace
var testAfter = test.replace(/^.+:/,'');
if you want to be free to use whatever delimiter, this String extension may do the work:
String.prototype.dataAfter = String.prototype.dataAfter ||
function(delim){
return this.split(delim)[1];
};
//usage
var testAfter = test.dataAfter(':');
var test2 = 'type#input';
var testAfter = test2.dataAfter('#');
What about substr?
var test2 = test.substr(test.indexOf(':')+1); // until end