Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 使用正则表达式解析HTTP URL 问题_Javascript_Regex - Fatal编程技术网

Javascript 使用正则表达式解析HTTP URL 问题

Javascript 使用正则表达式解析HTTP URL 问题,javascript,regex,Javascript,Regex,我有一个长字符串,格式如下: '{ "method": "POST", "url": "/iot/pipe/", "query": {}, "body": { "d": {"l": 1523737659, "n": "861359030665564", "b": 100, "v": "02.45", "t": 3, "dev": {"vr":7, "ae":1, "at":5, "ad":2, "as":4, "al":60, "tp":60, "tr":3, "tu":"http"://bus

我有一个长字符串,格式如下:

'{ "method": "POST", "url": "/iot/pipe/", "query": {}, "body": { "d": {"l": 1523737659, "n": "861359030665564", "b": 100, "v": "02.45", "t": 3, "dev": {"vr":7, "ae":1, "at":5, "ad":2, "as":4, "al":60, "tp":60, "tr":3, "tu":"http"://bus.mapit.me/iot/pipe/, "gt":50, "gm":120, "gh":400, "gs":3, "gr":2, "gg":1, "ua":0, "uu":"http"://bus.mapit.me/firmware/, "le":0, "lt":0, "sw":mapit2_v245, "sp":240, "rt":0, "sa":1}}}, "headers": { "host": "node_session_iot", "connection": "close", "content-length": "298", "accept": "*/*", "user-agent": "QUECTEL_MODULE", "content-type": "application/x-www-form-urlencoded" } }'
其内部包含URL,如下面的示例所示:

"uu":"http"://bus.mapit.me/firmware/
客观的 我的目标是使用
String.prototype.replace
和Regex将其转换为以下内容(注意
“http”
的末尾移动到字符串的末尾):

我试过的 为了实现这一点,我搜索了几个SO帖子,找到了以下代码,但代码不起作用:

str.replace(/\"http\":(\w+)/g, "\"http:$1\"");
这对所讨论的字符串没有任何影响。 我最接近的匹配是以下内容:

str.replace(/\"http\":/g, "\"http:\"");
它所做的并不是将
\“
移动到下一个位置,而不是将其移动到末端

问题: 我的正则表达式有什么问题?

工作代码:

var str='{“method”:“POST”,“url”:“/iot/pipe/”,“query”:“{}”,body:{“d”:{“l”:1523737659,“n”:“861359030665564”,“b”:100,“v”:“02.45”,“t”:3,“dev”:{“vr”:7,“ae”:1,“at”:5,“ad”:2,“as”:4,“al”:60,“tp”:60,“tr”:3,“tu”:“http://bus.mapit.me/iot/pipe/,“gt”:50,“gm”:120,“ghgr”:400,“gs”,2,“uu”:1“:“http:”://bus.mapit.me/firmware/,“le”:0,“lt”:0,“sw”:mapit2_v245,“sp”:240,“rt”:0,“sa”:1}}},,“headers”:{“host”:“node_session_iot”,“connection”:“close”,“content length”:“298”,“accept”:“*/*”,“user agent”:“QUECTEL_MODULE”,“content type”:“application/x-www-form-urlencoded”};
str=str.replace(/“http”([^,]*)/gm,““http$1”)

console.log(str)问题在于正则表达式与给定的URL不匹配。特别是,捕获组中的
\w+
意味着匹配一个或多个单词字符,其中单词字符是集合
[a-zA-Z0-9.]
中的任何字符。这与URL中的正斜杠不匹配,因为它们不被视为单词字符

相反,您可以使用
\S+
匹配任何非空白字符序列。此外,如果需要多次使用,请将结果重新分配给
str
(或其他变量)

str = '"uu":"http"://bus.mapit.me/firmware/';
new_str = str.replace(/\"http\":(\S+)/g, "\"http:$1\"");
console.log(new_str);
输出:

"uu":"http://bus.mapit.me/firmware/"

您可以尝试一下。

欢迎向下投票解释,我想知道答案有什么问题。不是向下投票,而是您的输出错误,
$1
替换中的
$1
,它意味着第一个捕获组,没有。您可以自己尝试该片段,它正在打印
“uu”:http://bus.mapit.me/firmware/“
据我所知,这是所需的输出。仅当源字符串中没有其他内容时,您的示例才有效。就您不知道结尾而言,您的示例完全是错误的。实际上,您的答案只是猜测……字符串”包含URL“,这将匹配
http
之后的所有内容,可能会有大量数据,但是毫无疑问,我们不知道如何匹配url尾注
”http://foo“
包含的斜杠字符不是
\w
的一部分。您至少应该将其更改为
/\“http\”:\/\/(\w+)/g
,但这将只匹配您的域,即从url的第一部分到下一个
/
。对于许多与正则表达式相关的问题是一个很好的资源。@PeterB我想要一个新字符串。这是有意的!
"uu":"http://bus.mapit.me/firmware/"