Jump to content

deleting ("var") from a string !


lofaifa

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.