这个问题可能已经被问过一百万次了,我可能离我下面的内容还很远,但我在任何地方都找不到它。我需要在下面获取警报以显示括号内的值。
//the element(thisId) holds the following string: id[33]
var thisId = $(this).attr('id');
var idNum = new RegExp("\[(.*?)\]");
alert(idNum);
我需要警报来显示值 33
。
最佳答案
您需要将字符串与正则表达式进行匹配,而不仅仅是创建正则表达式。这将返回一个包含完整匹配项和捕获组匹配项的数组。
var thisId = 'id[33]';
var match = thisId.match(/\[(.*?)\]/);
alert(match[1]); // Show first capture
关于javascript - jquery使用正则表达式获取括号中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197170/