Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have a string btnHome. How can I remove btn and only get Home from it in javascript / jQuery?
btnHome
btn
Home
I used slice like str.slice(3, -1) but it gives me Hom whereas I need Home.
str.slice(3, -1)
Hom
Remove the -1 part (just use str.slice(3)).
-1
str.slice(3)
The -1 argument says slice to 1 character from the end of the string.
Add a comment
Try like
alert(str.slice(3));
Need not to give -1 there.So it will slice it from starting of the string.
starting
You should use substr() method:
target.substr(3);
Demo
var txt="btnHome"; txt.slice(3,txt.length)
By using substring
txt.substring(3,txt.length);
Two simple ways to do it:
str.split('btn')[1]; // Home
str.slice(3); // Home
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.