-2

I need to match the elementId with any other sub element IDs that begin with the elementId. For example, Sub elements have their IDs marked as 1-1, 1-2,...,4-5,..., 8-13, etc. So, if the Element ID is 2, I need to get all the sub elements of this element by filtering (The result should be 2-1, 2-2, 2-3,...) I tried using this pattern but it doesn't work.

var regEx = '/['+elementId+']*/';
//elementsId is a var derived from a previous expression

In order to match this what is the RegExp pattern that I should use? I've provided the code segment below.

jsPlumb.draggable(newAgent, {
                containment: 'parent'
            });
            newAgent.on('click', '.cancel', function (e) {

                alert( newAgent.attr('id'));
                var elementId= newAgent.attr('id');
                var regEx = '/['+elementId+']*/';
                alert (regEx);

                for(var m=0; m<list.length; m++)
                {
                    if(list[m][0].exec(regEx) || list[m][1].exec(regEx))
                    {
                        alert('Deleting connection between ' + list[m][0] + '&' + list[m][1]);
                    }
                }

                jsPlumb.detachAllConnections(newAgent.attr('id'));
                jsPlumb.removeAllEndpoints(newAgent.attr('id'));
                jsPlumb.detach(newAgent.attr('id'));
                $(newAgent).remove();
            });
Nayantara Jeyaraj
  • 2,624
  • 7
  • 34
  • 63

2 Answers2

0

You can build regexp using a constructor, knowing that you need to escape manually special characters: link

var ids = ["1-1", "2-1", "2-2", "3-1", "3-2", "3-3", "3-4", "3-5"];

var buildRegex = function(id) {
  return new RegExp(id + '\\-\\d+');
}

for (var i = 1; i <= 3; ++i) {
  var reg = buildRegex(i);
  for (var k = 0; k < ids.length; ++k) {
    if (reg.test(ids[k])) {
      console.log(ids[k] + " is a sub element of " + i);
    }
  }
}
Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
0

I think your Regex should be equal to :'^' + elementId + '-'.
this will check for elements that starts with elementid-

Triple.B
  • 144
  • 1
  • 1
  • 7