Search code examples
javascriptregexreplaceparentheses

Javascript ,how to remove values before parentheses and also the parentheses it self


 var s = "1236(75)";
 var s = s.replace(/\(|\)/g, '');

 alert (s); // this gives me 123675  


what i actually need is 75

any help will be appreciated!

the above code results in 123675, but I need it to return just 75, please help


Solution

  • Use ^.+ to match everything from the start of the input string.

    var s = "1236(75)";
    var s = s.replace(/^.*\(|\)/g, '');
    

    That said, you could use a regex to do the opposite: instead of getting rid of everything you don't want, just match the parts you do want:

    var match = s.match(/\((\d+)|\)/)[1];