(javascript+;jquery+;regex)如何在更改选择框时警告regex值?
我试图在所选框发生更改时提醒regexp值, 选择框的值如下所示 #bla:123#bla2:12345 #bla:122#bla2:12111 #bla:663#bla2:93399 我使用Jquery获取选择框值,下面是我一直在尝试的代码:(javascript+;jquery+;regex)如何在更改选择框时警告regex值?,javascript,jquery,Javascript,Jquery,我试图在所选框发生更改时提醒regexp值, 选择框的值如下所示 #bla:123#bla2:12345 #bla:122#bla2:12111 #bla:663#bla2:93399 我使用Jquery获取选择框值,下面是我一直在尝试的代码: $(document).ready(function() { $('#idNrel').change(function() { var re = /#.*:(.*)#.*:(.*
$(document).ready(function() {
$('#idNrel').change(function()
{
var re = /#.*:(.*)#.*:(.*)/;
var sourcestring = $('#idNrel').val();
var results = [];
var i = 0;
for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
results[i] = matches;
for (var j=0; j<matches.length; j++) {
alert("results["+i+"]["+j+"] = " + results[i][j]);
}
i++;
}
});
});
$(文档).ready(函数(){
$('#idNrel').change(function()
{
变量re=/#.*:(.*)#.*:(.*)/;
var sourcestring=$('#idNrel').val();
var结果=[];
var i=0;
for(var matches=re.exec(sourcestring);matches!=null;matches=re.exec(sourcestring)){
结果[i]=匹配项;
对于(var j=0;j,正如前面所指出的,不需要两个循环,因为匹配的是一个捕获数组。类似这样的内容就足够了:
$(document).ready(function () {
$('#idNrel').change(function () {
var self = $(this),
r = /#[^:]+:([^#]+)#[^:]+:(.*)/g, // use global flag
sourcestring = self.val(),
matches = r.exec(sourcestring),
i = 0;
for (i = 0; i < matches.length; i += 1) {
// first element in the matches array will be the whole matching string
// second element in the matches array is the first capture group
// third element in the matches array is the second capture group
// this pattern continues, although you have no more capture groups
alert("Capture " + i + ": = " + matches[i]);
}
});
});
$(文档).ready(函数(){
$('#idNrel')。更改(函数(){
var self=$(此),
r=/#[^:::+:([^#]+)#[^:::+:(.*)/g,//使用全局标志
sourcestring=self.val(),
matches=r.exec(sourcestring),
i=0;
对于(i=0;i
当然,还有一把小提琴:
您可以在此处阅读有关捕获组和JavaScript正则表达式的更多信息:
您不需要循环。match
应该能够将那些捕获组提取到一个数组中。