Skip to content Skip to sidebar Skip to footer

Javascript Regular Expression To Remove Trailing Numbers In Parenthesis

How can I replace/append something (22) string to something, using some kind of expression matching something is not a constant its always different but the part (integer) is alway

Solution 1:

Here it is:

var text = "Java (77) Javascript (22) Car (11) Carpet (15)";
text.replace(/(\w+) \(\d+\)/g,"$1");   // returns "Java Javascript Car Carpet"

Solution 2:

Assuming something as \w -> A-Za-z0-9_, this will convert something (22) to something

yourstring=yourstring.replace(/(\w+)\s*\(\d+\)/g,"$1")

or just remove numbers in brackets

yourstring=yourstring.replace(/\s*\(\d+\)/g, "")

Post a Comment for "Javascript Regular Expression To Remove Trailing Numbers In Parenthesis"