Axeia Posted May 6, 2009 Share Posted May 6, 2009 Using the mozilla javascript reference as my guideline I thought this would work, but it doesn't. (s is the string) var regex = /<input.*id='([^'])[^>]*/; var arr = regex.exec( s ); if( arr != null) alert( arr[1] ); String is like this: <input value='whateverrrrrrrrr' id='g29' name='download[]' type='checkbox'/> I'd love to hear any optimisations I could make as well as this thing is gonna be used on several hundred strings. (and in that light it might be useful to know that the id will always be g followed by numbers). Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 var regex = /id=[\'\"]{0,1}(\w+)/; var arr = regex.exec(s); if (arr !== null) alert(arr[1]); Like that? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted May 6, 2009 Share Posted May 6, 2009 Can you instead gather all the <input> tags and then get value of the id attribute? You are not clear if it will only be from <input> tags, and if you only want the id attribute's value. I'm talking about these functions: getElementsByTagName() getAttribute('name') Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 6, 2009 Author Share Posted May 6, 2009 Can't do either as it's for adding a custom parser to jquery's tablesorter plugin. it will only be input fields, but the plugin converts the contents of the tablecell to a string. So not much choice but to use a regular expression, or come to think of it.. creating a new node using .innerHTML, but I think that would be slower. Anyhow Ken2k7's code works, any idea as to what's wrong with my regular expression? Did I use some feature not supported by javascripts implementation of regular expressions? And for the the curious, the total sorter code has become: $.tablesorter.addParser({ id: 'input', is: function(s) { return false; }, format: function(s) { var regex = /id=[\'\"]{0,1}(\w+)/; var arr = regex.exec(s); return document.getElementById( arr[1] ).checked; }, type: "numeric" }); Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Yes, there is an issue with your Regex, but not with anything particularly wrong. I just though mine is shorter. var regex = /<input.*id='([^'])[^>]*/; See where you have ([^'])? Well, using that set, you're matching only once. You need a + or a * to match more than once. So it should be ([^']*). You need the * rather than + in case the id field is empty. Quote Link to comment Share on other sites More sharing options...
Axeia Posted May 6, 2009 Author Share Posted May 6, 2009 A yes, so easy to see once pointed out. Thanks again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.