lofaifa Posted March 2, 2012 Share Posted March 2, 2012 basically i want to delete anything inside the () and "()" .. i have just numbers but they varie var tag='html5 (2) ' ; var tag='html5 (5) ' ; var tag='html5 (46) ' ; i want it to become = html5 tag=tag.replace(/\d+/g,''); tag=tag.replace('()',''); doesnt work cuz 5 also has been deleted .. what to do ? Quote Link to comment https://forums.phpfreaks.com/topic/258150-deleting-var-from-a-string/ Share on other sites More sharing options...
joe92 Posted March 3, 2012 Share Posted March 3, 2012 You needn't run two replace commands for what can be done in one. You may have tried including the brackets before but were unaware that they have a special meaning in regular expressions. They represent capturing parenthesis. An extremely useful tool. If you want to match them literally you have to escape them in the pattern with a backslash. Change your replace lines to the following one: tag = tag.replace(/\(\d+\)/g,''); This won't get the 5 in html5 as it is not surrounded by round brackets. Hope that helps you, Joe Quote Link to comment https://forums.phpfreaks.com/topic/258150-deleting-var-from-a-string/#findComment-1323331 Share on other sites More sharing options...
lofaifa Posted March 3, 2012 Author Share Posted March 3, 2012 thank you it worked perfectly ! Quote Link to comment https://forums.phpfreaks.com/topic/258150-deleting-var-from-a-string/#findComment-1323335 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.