gpt4 book ai didi

javascript - 在 Javascript 中解析复杂字符串的最有效方法

转载 作者:行者123 更新时间:2023-11-30 12:44:26 25 4
gpt4 key购买 nike

我在解析这个字符串时遇到问题。

newParams=[ "3","2","img1x:51","img1y:111","img1h:209","img1w:330","img1Px:231","img1Py:291","img1Dx:44","img1Dy:104","img2x:51","img2y:331","img2h:100","img2w:329","img2Px:274","img2Py:408","img2Dx:44","img2Dy:324","txt1x:399","txt1y:119","txt1h:103","txt1w:303","txtBox1x:391","txtBox1y:111","txtBox1h:119","txtBox1w:319","txt1Px:679","txt1Py:199","txt1Dx:392","txt1Dy:112","txt2x:399","txt2y:249","txt2h:103","txt2w:303","txtBox2x:391","txtBox2y:241","txtBox2h:119","txtBox2w:319","txt2Px:679","txt2Py:329","txt2Dx:392","txt2Dy:242","txt3x:399","txt3y:379","txt3h:44","txt3w:304","txtBox3x:391","txtBox3y:371","txtBox3h:60","txtBox3w:320","txt3Px:680","txt3Py:409","txt3Dx:392","txt3Dy:372"]; 

该字符串包含我需要在 DOM 中定位和调整大小的文本对象和图像对象的高度、宽度、x 和 y 坐标。

数组中的前 2 个值是文本字段的数量和图像的数量(我将在循环中将其用作最大迭代次数)。

我需要解析数字值,并将它们放入这些单独的数组中。

var textXcoords  = new Array();
var textYcoords = new Array();
var textHeight = new Array();
var textWidth = new Array();

var txtBoxXcoords = new Array();
var txtBoxYcoords = new Array();
var txtBoxHeight = new Array();
var txtBoxWidth = new Array();

var textPullXcoords = new Array();
var textPullYcoords = new Array();
var textDragXcoords = new Array();
var textDragYcoords = new Array();

var imgXcoords = new Array();
var imgYcoords = new Array();
var imgHeight = new Array();
var imgWidth = new Array();

var imgPullXcoords = new Array();
var imgPullYcoords = new Array();
var imgDragXcoords = new Array();
var imgDragYcoords = new Array();

例如在“img1x:51”(图像 1,Y 坐标)中,数字 51 将被插入 imgXcoords 数组在位置 0

在“img2y:111”(图像 2,Y 坐标)中,数字 111 将被插入 imgYcoords 数组位置1

在“txt2w:303”(文本字段 2,宽度)中,数字 303 将被插入 txtBoxWidth 数组的位置1.

如果有人可以告诉我如何使用循环将 img(i)x 和 img(i)y 值推送到它们的数组(imgXcoords 和 imgYcoords)中,我可以从那里找出其余部分。

我只是不知道在字符串中搜索我要查找的内容并将其插入适当数组的最有效方法。

我尝试了图像的 x 坐标,但我的开始和结束位置相差甚远。

var iX1 = newParams.indexOf("img"+(1)+"x");
var iX2 = (iX1 + 7);
var res = newParams.substr(iX2,(iX2+2));

如果我将“res”发送到 console.log,我会得到:1","img1y:111","im 结果。

当我将它放入一个循环中,并用 var i=0 替换“1”时,它变得更加古怪。

这是我尝试的一个 fiddle :http://jsfiddle.net/S9RGH/

最佳答案

split 是你的 friend 。看看我在这里所做的是否有帮助:jsfiddle

您可以使用split 将字符串分割成单独的名称/值对,并将它们放入一个数组中以便于访问。

关于javascript - 在 Javascript 中解析复杂字符串的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23098253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com