这可能已被问过一百万次了,而且我可能已经完成了以下的工作,但我无法在任何地方找到它。我需要在下方收到提醒,以显示括号内的值。
//the element(thisId) holds the following string: id[33]
var thisId = $(this).attr('id');
var idNum = new RegExp("\[(.*?)\]");
alert(idNum);
我需要提醒以显示值33
。
答案 0 :(得分:1)
您可以使用exec()
获取正则表达式的匹配项:
var thisId = 'id[33]';
var matches = /\[(.*?)\]/g.exec(thisId);
alert(matches[1]); // you want the first group captured
答案 1 :(得分:1)
您需要将字符串与正则表达式匹配,而不仅仅是创建正则表达式。这将返回一个包含完整匹配和捕获组匹配的数组。
var thisId = 'id[33]';
var match = thisId.match(/\[(.*?)\]/);
alert(match[1]); // Show first capture