Jump to content

Recommended Posts

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 ?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/258150-deleting-var-from-a-string/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.