The Letter E Posted May 11, 2012 Share Posted May 11, 2012 I can't figure out why this regular expression is not working. I had it working before when it was completely hard coded, now with the new dynamic regexp it's failing. I alert'd the regexp out and it looks the same, far as i can tell. Any help is much appreciated! addnum = 1; handle = 'item'; name = 'item[0]'; pattern = "/^"+handle+"\\[\\d+\\]/"; var regex = new RegExp(pattern); name = name.replace(regex, handle+"["+addnum+"]"); the first 3 variables are placeholders to help visualize the problem, but it should work with many different options. Thanks in advance, E Quote Link to comment https://forums.phpfreaks.com/topic/262384-js-dynamic-regexp/ Share on other sites More sharing options...
requinix Posted May 11, 2012 Share Posted May 11, 2012 Only use //s when you're writing the expression as an object, inline. pattern = /^handle\[\d+\]/; (that won't work, of course) If as a string then leave them out. pattern = "^" + handle + "\\[\\d+\\]"; Quote Link to comment https://forums.phpfreaks.com/topic/262384-js-dynamic-regexp/#findComment-1344693 Share on other sites More sharing options...
The Letter E Posted May 11, 2012 Author Share Posted May 11, 2012 That, I did not know. Thank you for that helpful bit of knowledge! It is now working perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/262384-js-dynamic-regexp/#findComment-1344800 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.