如何在类似于 PHP 的 preg_match_all() 的 JavaScript 中使用正则表达式匹配多次出现? [英] How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg_match_all()?

查看:26
本文介绍了如何在类似于 PHP 的 preg_match_all() 的 JavaScript 中使用正则表达式匹配多次出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析由 && 分隔的键=值对组成的 url 编码字符串.

以下只会匹配第一次出现,将键和值分解为单独的结果元素:

var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/)

字符串 '1111342=Adam%20Franco&348572=Bob%20Jones' 的结果是:

['1111342', 'Adam%20Franco']

使用全局标志 'g' 将匹配所有匹配项,但只返回完全匹配的子字符串,而不是分隔的键和值:

var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/g)

字符串 '1111342=Adam%20Franco&348572=Bob%20Jones' 的结果是:

['1111342=Adam%20Franco', '&348572=Bob%20Jones']

虽然我可以在 & 上拆分字符串并单独拆分每个键/值对,但有没有办法使用 JavaScript 的正则表达式支持来匹配模式 /(?:&|&)?([^=]+)=([^&]+)/ 类似于PHP的preg_match_all()函数?

我的目标是通过某种方式获得子匹配的结果,例如:

[['1111342', '348572'], ['Adam%20Franco', 'Bob%20Jones']]

[['1111342', 'Adam%20Franco'], ['348572', 'Bob%20Jones']]

解决方案

从评论中提升

<块引用>

2020 评论:我们现在有 ,而不是使用正则表达式URLSearchParams,它为我们完成了所有这些,因此不再需要自定义代码,更不用说正则表达式了.

迈克 'Pomax' Kamermans

此处列出了浏览器支持https://caniuse.com/#feat=urlsearchparams<小时>

我建议使用替代正则表达式,使用子组分别捕获参数的名称和值,re.exec():

function getUrlParams(url) {var re =/(?:?|&(?:amp;)?)([^=&#]+)(?:=?([^&#]*))/g,匹配,参数 = {},decode = function (s) {return decodeURIComponent(s.replace(/+/g, " "));};if (typeof url == "undefined") url = document.location.href;while (match = re.exec(url)) {参数[解码(匹配[1])] = 解码(匹配[2]);}返回参数;}var result = getUrlParams("http://maps.google.de/maps?f=q&source=s_q&hl=de&geocode=&q=Frankfurt+am+Main&sll=50.106047,8.679886&sspn=0.370369,0.833588&ie=UTF8&ll=50.116616,8.680573&spn=0.35972,0.833588&z=11&iwloc=addr");

result 是一个对象:

<前>{f: "q"地理编码:"hl:德"即:UTF8"iwloc:地址"ll:50.116616,8.680573"q:法兰克福"sll:50.106047,8.679886"来源:s_q"spn:0.35972,0.833588"sspn:0.370369,0.833588"z:11"}

正则表达式分解如下:

<前>(?: # 非捕获组?|& # "?"或者 "&"(?:amp;)?#(允许&amp;",用于错误的 HTML 编码 URL)) # 结束非捕获组( # 第 1 组[^=&#]+ # 除="、&"或#"之外的任何字符;至少一次) # end group 1 - 这将是参数的名称(?: # 非捕获组=?# 一个=",可选( # 第 2 组[^&#]* # 除&"或#"之外的任何字符;任意次数) # end group 2 - 这将是参数的值) # 结束非捕获组

I am trying to parse url-encoded strings that are made up of key=value pairs separated by either & or &amp;.

The following will only match the first occurrence, breaking apart the keys and values into separate result elements:

var result = mystring.match(/(?:&|&amp;)?([^=]+)=([^&]+)/)

The results for the string '1111342=Adam%20Franco&348572=Bob%20Jones' would be:

['1111342', 'Adam%20Franco']

Using the global flag, 'g', will match all occurrences, but only return the fully matched sub-strings, not the separated keys and values:

var result = mystring.match(/(?:&|&amp;)?([^=]+)=([^&]+)/g)

The results for the string '1111342=Adam%20Franco&348572=Bob%20Jones' would be:

['1111342=Adam%20Franco', '&348572=Bob%20Jones']

While I could split the string on & and break apart each key/value pair individually, is there any way using JavaScript's regular expression support to match multiple occurrences of the pattern /(?:&|&amp;)?([^=]+)=([^&]+)/ similar to PHP's preg_match_all() function?

I'm aiming for some way to get results with the sub-matches separated like:

[['1111342', '348572'], ['Adam%20Franco', 'Bob%20Jones']]

or

[['1111342', 'Adam%20Franco'], ['348572', 'Bob%20Jones']]

解决方案

Hoisted from the comments

2020 comment: rather than using regex, we now have URLSearchParams, which does all of this for us, so no custom code, let alone regex, are necessary anymore.

Mike 'Pomax' Kamermans

Browser support is listed here https://caniuse.com/#feat=urlsearchparams


I would suggest an alternative regex, using sub-groups to capture name and value of the parameters individually and re.exec():

function getUrlParams(url) {
  var re = /(?:?|&(?:amp;)?)([^=&#]+)(?:=?([^&#]*))/g,
      match, params = {},
      decode = function (s) {return decodeURIComponent(s.replace(/+/g, " "));};

  if (typeof url == "undefined") url = document.location.href;

  while (match = re.exec(url)) {
    params[decode(match[1])] = decode(match[2]);
  }
  return params;
}

var result = getUrlParams("http://maps.google.de/maps?f=q&source=s_q&hl=de&geocode=&q=Frankfurt+am+Main&sll=50.106047,8.679886&sspn=0.370369,0.833588&ie=UTF8&ll=50.116616,8.680573&spn=0.35972,0.833588&z=11&iwloc=addr");

result is an object:

{
  f: "q"
  geocode: ""
  hl: "de"
  ie: "UTF8"
  iwloc: "addr"
  ll: "50.116616,8.680573"
  q: "Frankfurt am Main"
  sll: "50.106047,8.679886"
  source: "s_q"
  spn: "0.35972,0.833588"
  sspn: "0.370369,0.833588"
  z: "11"
}

The regex breaks down as follows:

(?:            # non-capturing group
  ?|&         #   "?" or "&"
  (?:amp;)?    #   (allow "&amp;", for wrongly HTML-encoded URLs)
)              # end non-capturing group
(              # group 1
  [^=&#]+      #   any character except "=", "&" or "#"; at least once
)              # end group 1 - this will be the parameter's name
(?:            # non-capturing group
  =?           #   an "=", optional
  (            #   group 2
    [^&#]*     #     any character except "&" or "#"; any number of times
  )            #   end group 2 - this will be the parameter's value
)              # end non-capturing group

这篇关于如何在类似于 PHP 的 preg_match_all() 的 JavaScript 中使用正则表达式匹配多次出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆