Copy link to clipboard
Copied
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.
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
![]()
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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
![]()
Copy link to clipboard
Copied
the king of regExp does it again!!!
Copy link to clipboard
Copied
Thanks ... that worked...
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more