General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion how to check if an attribute list contains a string.

  • nileshp

    how to check if an attribute list contains a string.

    Participants 0
    Replies 1
    Last Activity 9 years, 2 months ago

    Hi,
      I'm working on a problem where I need to check if the attribute of a tag contains a particular string. I'm able to get the list of attributes for a specific element by using the getAttribute() method. This returns the string containing the attribute value(s) for this attribute.

    I want to check if the attribute value  that i get from the getAttribute() method contains a string (so that I don't add duplicate attribute). Is there a method i could use to do this.

    Basically, I want a function that lets me check something like .Contains(“blah”).

    Any help would be greatly appreciated.

    Thanks,
    Nilesh

    Reply

    Derek Read

    Reply to: how to check if an attribute list contains a string.

    XMetaL doesn't expose any APIs for string manipulation. However, there is no need as all scripting languages have a string object of some kind with associated methods and properties.

    Please have a look at Microsoft's documentation for the Windows JScript engine and Windows Script Host. Most likely the version you have installed will be 5.7, 5.8 or possibly 9. Microsoft has an entire section devoted to each object, including their String object. There are also a lot of books out there that are very good at explaining JScript (JavaScript / ECMAScript) in detail, so you might want to look at those. One of the best (I think) is “Pure JavaScript” from SAMS publishing if you can find it.

    It really should be a prerequisite to know the scripting engine / language you are using to create any XMetaL customization as it will make things a lot easier.

    Now, to answer your question, you can use something like the following, assuming the value you want to check is in a variable named attrval (which I redefine here):

    //XMetaL Script Language JScript:
    var attrval = "testing123";
    var stringtofind = "123";
    var pos = attrval.indexOf(stringtofind);
    if(pos > -1) {
    Application.Alert("attrval contains the string '" + stringtofind + "' at position " + pos);
    }
    else {
    Application.Alert("string not found");
    }

    There are going to be quite a few other ways to accomplish the same thing, that one is simply the most direct. Here's another example that uses a the regular expression search method:

    //XMetaL Script Language JScript:
    var attrval = "testing123";
    var regex = /123/;
    var pos = attrval.search(regex);
    if(pos > -1) {
    Application.Alert("found a match for the regular expression '" + regex + "' at position " + pos);
    }
    else {
    Application.Alert("no match");
    }

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support