Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

match pattern and get the position using javascript in illustrator.

Explorer ,
Apr 18, 2018 Apr 18, 2018

Hi,

I need to get the position of a pattern in a text and store it in an array.

for example, consider the string

a^{^{x^{1}}}

I need to get all the positions of '{' and '}' to be stored in an array named supB;

Thanks in advance for the help.

TOPICS
Scripting
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 19, 2018 Apr 19, 2018

Hmmh?

I hope I understand you right. How about:

var aCon = "a^{^{x^{1}}}";

var reg = /[{}]/g;

var res;

while (res = reg.exec(aCon)) {

    $.writeln(res[0] + " | " + res.index);

}

If so, have fun

Translate
Adobe
LEGEND ,
Apr 19, 2018 Apr 19, 2018

I'm not clear what you are having issues with. A simple loop counting through the characters and using  .push() to add them to an array will do.

Mylenium

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2018 Apr 19, 2018

if(documents.length >0){  //if document is open

    if(app.activeDocument.selection[0]){  //if a line is selected

        var supBPosition=[];

        var subBPosition=[];

        var doc = app.activeDocument;  //getting the active document

        var line = doc.pathItems[0];  //getting path items from the active document

        var firstPoint = line.pathPoints[0].anchor; // [x, y]

        var lastPoint = line.pathPoints[line.pathPoints.length - 1].anchor; // [x, y]      

        var eqValues = (prompt("Enter something ", "", "")); //getting the values to write

        var eqValuesCopy=eqValues;

        var startCharacterSize=0;

        var ys=[];

        for(var i=0;i<eqValuesCopy.length;i++){

            if(eqValuesCopy.indexOf("{")>=0){

                ys.push(eqValuesCopy.indexOf("{"));

                startCharacterSize=eqValuesCopy.indexOf("{");

                eqValuesCopy=eqValuesCopy.slice(startCharacterSize);

            }

        }

    }

}

I don't get whats wrong in my loop

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Apr 19, 2018 Apr 19, 2018

I'm not 100% sure what you need this to do, but based on my assumptions from this post as well as your other post about equations, i put this together. This will give you two arrays, one for open brackets and one for close brackets. This should allow you to target specific textRanges by character index and apply the necessary characterAttributes for subscript, superscript etc.

Of course some error handling and validation of the user's input would be quite necessary, but this should point you in the right direction, i hope.

function test()

{

    var supBPosition = [];

    var subBPosition = [];

    var doc = app.activeDocument; //getting the active document 

    var line = doc.pathItems[0]; //getting path items from the active document  

    var firstPoint = line.pathPoints[0].anchor; // [x, y] 

    var lastPoint = line.pathPoints[line.pathPoints.length - 1].anchor; // [x, y]        

    var eqValues = (prompt("Enter something ", "", "")); //getting the values to write 

    var openBrackets = [];

    var closeBrackets = [];

    for(var x=0,len=eqValues.length;x<len;x++)

    {

        if(eqValues === "{")

        {

            openBrackets.push(x);

        }

        else if(eqValues === "}")

        {

            closeBrackets.push(x);

        }

    }

    alert("Results:\nThere are open brackets at the following indexes: " + openBrackets +

        "\nThere are close brackets at the following indexes: " + closeBrackets);

}

if(app.documents.length && app.activeDocument.selection.length)

{

    test();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2018 Apr 19, 2018

Hmmh?

I hope I understand you right. How about:

var aCon = "a^{^{x^{1}}}";

var reg = /[{}]/g;

var res;

while (res = reg.exec(aCon)) {

    $.writeln(res[0] + " | " + res.index);

}

If so, have fun

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2018 Apr 20, 2018

the king of regExp does it again!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 25, 2018 Apr 25, 2018
LATEST

Thanks ... that worked...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines