Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 这个String.Replace方法在做什么?_Javascript_Regex_String_Xojo - Fatal编程技术网

Javascript 这个String.Replace方法在做什么?

Javascript 这个String.Replace方法在做什么?,javascript,regex,string,xojo,Javascript,Regex,String,Xojo,我正在尝试将此Javascript代码移植到另一种语言(Xojo): 我知道路径应该是一个包含x-y对数字的字符串(采用SVG格式,如“l5025l10010”)。我认为这会将前面的字符串拆分为两个对象50,25和100,10,然后将它们推到points数组上,对吗?不确定您是否知道/L?\s*([\-\d\.e]+)[\s,]*([\-\d\.e]+)*/ig是正则搜索字符串 如果我这么快地解密,我会说它在开始时寻找一个可选的“L”,然后是可选的空白,然后是一个数字(即任何包含数字、句点、减号

我正在尝试将此Javascript代码移植到另一种语言(Xojo):


我知道路径应该是一个包含x-y对数字的字符串(采用SVG格式,如
“l5025l10010”
)。我认为这会将前面的字符串拆分为两个对象
50,25
100,10
,然后将它们推到points数组上,对吗?

不确定您是否知道
/L?\s*([\-\d\.e]+)[\s,]*([\-\d\.e]+)*/ig
是正则搜索字符串

如果我这么快地解密,我会说它在开始时寻找一个可选的“L”,然后是可选的空白,然后是一个数字(即任何包含数字、句点、减号或“e”)、空白或逗号、另一个数字,以及数字周围的括号意味着这些是传递给
replace()中的函数的匹配值
打电话。末尾的“ig”是选项,“i”表示忽略案例IIRC

因此,该代码似乎只获取两个数字,可以选择前面加一个“L”。然后,它将这些数字读入x和y,并将它们附加到
数组中


在Xojo中,您必须在循环中(Xojo不支持本地函数)使用RegEx类重复解析字符串。要将扫描的数字转换为双精度数字,请使用
Val()
函数。

虽然问题中的正则表达式将匹配单个“L”字母后面的两个数字,但它也将匹配许多无法解析为有效数字的其他内容。 您是正确的,这段代码将创建两个对象,它们的X和Y属性由匹配的数字填充

使用Realbasic.Points保存坐标,可以通过以下Xojo代码段实现此功能:

Dim inString As String = "L 50 25 L 100 10"
Dim outArray(-1) As Realbasic.Point

Dim rx As New RegEx()
Dim match As RegExMatch
rx.SearchPattern = "L?\s*([\-\d.e]+)[\s,]*([\-\d.e]+)*"
match = rx.Search(inString) // Get the first matching part of the string, or nil if there's no match
While match <> nil
    dim x As Integer = match.SubExpressionString(1).Val() // X coordinate is captured by the first group
    dim y As Integer = match.SubExpressionString(2).Val() // Y coordinate is captured by the second group
    dim p As new REALbasic.Point(x, y)
    outArray.Append(p)
    match = rx.Search() // Get the next matching part, or nil if there's no more
Wend

原始模式将匹配“Lee…”,此更新模式将在“L”和数字之间至少需要一个空格,并且可能是数字的一部分的字符将不匹配,但是它们并不构成有效的数字表示。

这里的
x
y
分别是第一组和第二组捕获的组。并将它们作为对象添加到
points
数组中。@Garry你说得对。@Garry,如果你不想告诉我们你将它移植到哪种语言,我想你可以删除这篇文章。我正在移植到Xojo。编辑我的问题以反映。
Dim inString As String = "L 50 25 L 100 10"
Dim outArray(-1) As Realbasic.Point

Dim rx As New RegEx()
Dim match As RegExMatch
rx.SearchPattern = "L?\s*([\-\d.e]+)[\s,]*([\-\d.e]+)*"
match = rx.Search(inString) // Get the first matching part of the string, or nil if there's no match
While match <> nil
    dim x As Integer = match.SubExpressionString(1).Val() // X coordinate is captured by the first group
    dim y As Integer = match.SubExpressionString(2).Val() // Y coordinate is captured by the second group
    dim p As new REALbasic.Point(x, y)
    outArray.Append(p)
    match = rx.Search() // Get the next matching part, or nil if there's no more
Wend
"L?\s+(\-?\d+(?:\.\d+)?(?:e-?\d+)?)[\s,]+(\-?\d+(?:\.\d+)?(?:e-?\d+)?)"