Get string inside parentheses, removing parentheses, with regex

后端 未结 3 1449
甜味超标
甜味超标 2020-12-11 22:03

I have these two strings...

var str1 = \"this is (1) test\";
var str2 = \"this is (2) test\";

And want to write a RegEx to extract what is

3条回答
  •  情书的邮戳
    2020-12-11 22:47

    try this

        var re = (/\((.*?)\)/g);
    
        var str1 = str1.match(/\((.*?)\)/g);
        var new_str1=str1.substr(1,str1.length-1);
        var str2 = str2.match(/\((.*?)\)/g);
        var new_str2=str2.substr(1,str2.length-1);
    
        var str3 = new_str1+new_str2; //produces "12"
    

提交回复
热议问题