How can I get the first three letters of a string in JQuery?
For example: Turn cat1234 to cat
How can I get the first three letters of a string in JQuery?
For example: Turn cat1234 to cat
No jQuery needed! Just use the substring method:
var name = "cat1234"
var variable2 = name.substring(0, 3);
Use .slice(start, end)
with start and end values:
var str = 'cat1234';
document.body.innerHTML = str.slice(0, 3);
With javascript you can use a regular expression with .match()
method to exclude the number and get the string value.
var str ='cat1234',
rg = /[a-zA-Z]+/g,
ns = str.match(rg);
document.body.innerHTML = ns[0];
you could also do:
var str = "cat1234";
console.log( str.substr(0, 3));
Use substring():
"cat1234".substring(0,3);
It means "get all the characters starting at position 0 up to position 3".
Please note this is not a JQuery function, it's plain Java Script.