For some reason this is not working, yet all my searches say it should work.
I have a开发者_如何学运维n id on an element where I am trying to increment the value by 1.
so let's say I have
<input id="someText1">
I want to change the 1 to a 2, so I end up with
<input id="someText2">
I've tried to match just the text using
text = $("input").attr("id").match(/^[a-zA-Z]+$/);
But when I output the value of text, I get null
Please help!
Remove the $
sign (the text does end with a number and you're telling it that it ends with a letter)
Your regular expression should be
text = $("input").attr("id").match(/^[a-zA-Z0-9]+$/);
Are you after this?
var text = $("input").attr("id").replace(/[0-9]+$/, "");
var num = $("input").attr("id").replace(/^[a-zA-Z]+/, "");
The match
selector must equal the ID exactly, and as your regex matches any string containing ONLY a-z and A-Z, it doesn't match someText1
! You should try
^[a-zA-Z]+[0-9]$
I would suggest using a class name selector, such as $('.inputs')
as it will be faster than getting all the input elements on the page and then filtering by id (esp. in modern browsers).
精彩评论