admin管理员组文章数量:1291098
I have a dynamically formed string like - part1.abc.part2.abc.part3.abc
In this string I want to get the substring based on second to last occurrence of "."
so that I can get and part3.abc
Is there any direct method available to get this?
I have a dynamically formed string like - part1.abc.part2.abc.part3.abc
In this string I want to get the substring based on second to last occurrence of "."
so that I can get and part3.abc
Is there any direct method available to get this?
Share Improve this question asked Nov 5, 2013 at 5:03 OkkyOkky 10.5k15 gold badges77 silver badges123 bronze badges 1- Are the lengths of each part dynamic? Or are they known? – William Riley Commented Nov 5, 2013 at 5:08
2 Answers
Reset to default 8You could use:
'part1.abc.part2.abc.part3.abc'.split('.').splice(-2).join('.'); // 'part3.abc'
You don't need jQuery for this.
Nothing to do with jQuery. You can use a regular expression:
var re = /[^\.]+\.[^\.]+$/;
var match = s.match(re);
if (match) {
alert(match[0]);
}
or
'part1.abc.part2.abc.part3.abc'.match(/[^.]+\.[^.]+$/)[0];
but the first is more robust.
You could also use split and get the last two elements from the resulting array (if they exist).
本文标签: javascriptGet Second to last character position from string using jQueryStack Overflow
版权声明:本文标题:javascript - Get Second to last character position from string using jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741513306a2382734.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论