最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting elementscharacters to the right of another character - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 11

You 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
发布评论

评论列表(0)

  1. 暂无评论