Regular Expressions in JavaScript not working
Hello,
I am trying to use the native Regular Expression with Javascript to find a particlar TextNode in a SVG object. I have been trying the following code, but still do not get a matching result:
var sectionTest;
var sectionTest1;
var testText = "TextNode(5)";
var testText1 = "TextNode\(\d+\)";
var sectionRegExp = new RegExp(testText);
var sectionRegExp1 = new RegExp(testText1);
for(var tabLoop = 0; tabLoop < 1; tabLoop++)
{
broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).Refresh();
sectionCount = broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).ChildCount;
for(sectionLoop = 0; sectionLoop < sectionCount; sectionLoop++)
{
sectionCheck = broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).Child(sectionLoop);
sectionTest = sectionRegExp.test(sectionCheck.Name);
sectionTest1 = sectionRegExp1.test(sectionCheck.Name);
Log.Message(sectionCheck.Name);
Log.Message(sectionTest);
Log.Message(sectionTest1);
if(sectionTest == true || sectionTest1 == true)
Log.Message(sectionCheck.Name + ":" + sectionCheck.contentText);
}
}
The Children of the SVG object have TextNodes with Names from 0-12 and with some "id_#".
I am not sure what I am doing wrong here. As far as I can tell the RegExp is correct and should return a match.
Any help is welcomed. Thanks.
You could add an additional clause to the if to check is a number. Use isNaN (https://www.w3schools.com/jsref/jsref_isnan.asp)and, if it returns false, then it's a numeric value.