Is it possible to get all strings after a the first character?
var val = 'asdasd:111122:123123123';
var response = val.substring(val.lastIndexOf(":")+1);
alert(response ); // "123123123"
// Would like: ":111122:123123123"
Thank you!
Is it possible to get all strings after a the first character?
var val = 'asdasd:111122:123123123';
var response = val.substring(val.lastIndexOf(":")+1);
alert(response ); // "123123123"
// Would like: ":111122:123123123"
Thank you!
Share Improve this question asked May 31, 2016 at 21:53 BriBri 5932 gold badges5 silver badges23 bronze badges 2-
1
try
var response = val.substring(val.indexOf(":"));
– blurfus Commented May 31, 2016 at 21:55 -
2
just use
indexOf
rather thanlastIndexOf
:val.substring(val.indexOf(':'))
– Hamms Commented May 31, 2016 at 21:56
3 Answers
Reset to default 11Use indexOf(...)
instead of lastIndexOf(...)
If you want to include the ":"
then do not add one to the index.
Like this:
var val = 'asdasd:111122:123123123';
var response = val.substring(val.indexOf(":"));
console.log(response); // ":111122:123123123"
var mobileWithCode="+91-9842505145";//mobile value with nation code
mobile = mobileWithCode.substring(mobileWithCode.indexof("-"));
alert(mobile);
Just Remove +1 And Go ahead your code will work fine
var response = val.substring(val.lastIndexOf(":"));
alert(response); // Would Bee: ":111122:123123123"