6

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!

2
  • 1
    try var response = val.substring(val.indexOf(":"));
    – blurfus
    May 31, 2016 at 21:55
  • 2
    just use indexOf rather than lastIndexOf: val.substring(val.indexOf(':'))
    – Hamms
    May 31, 2016 at 21:56

3 Answers 3

10

Use 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"

0
var mobileWithCode="+91-9842505145";//mobile value with nation code

mobile = mobileWithCode.substring(mobileWithCode.indexof("-"));

alert(mobile);
-2

Just Remove +1 And Go ahead your code will work fine

var response = val.substring(val.lastIndexOf(":"));

alert(response); // Would Become: ":111122:123123123"
1
  • except he does not want the last instance of it - try it yourself
    – blurfus
    May 31, 2016 at 21:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.