preg_match_all JS 等效吗?

发布于 2024-07-24 07:59:37 字数 106 浏览 8 评论 0原文

Javascript 中是否有与 PHP 的 preg_match_all 等效的函数? 如果没有,将正则表达式的所有匹配项放入数组的最佳方法是什么? 我愿意使用任何 JS 库来让它变得更容易。

Is there an equivalent of PHP's preg_match_all in Javascript? If not, what would be the best way to get all matches of a regular expression into an array? I'm willing to use any JS library to make it easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

您的好友蓝忘机已上羡 2024-07-31 07:59:37

您可以将 match 与全局修饰符一起使用:

>>> '1 2 3 4'.match(/\d/g);
["1", "2", "3", "4"]

You can use match with the global modifier:

>>> '1 2 3 4'.match(/\d/g);
["1", "2", "3", "4"]
路弥 2024-07-31 07:59:37

John Resig 在他的博客上写了一项很棒的技术,名为“搜索但不替换'

它使用 javascript 的替换函数来工作,该函数接受一个回调函数,并且不返回任何内容以保持原始内容不变。

这比使用全局匹配和迭代结果数组更简洁,特别是当您捕获多个组时。

John Resig has written about a great technique on his blog called 'Search and dont replace'

It works using javascript's replace function, which takes a callback function, and returns nothing to leave the original content unaltered.

This can be a neater than using a global match and iterating over an array of results, especially if you're capturing several groups.

染柒℉ 2024-07-31 07:59:37

在 JS 中,与 PHP 中的 preg_match_all 更好的等效方法是使用 exec() 函数。 这也将允许您捕获组,而使用 match() 则无法做到这一点。

例如,您想要从变量 myString 中捕获所有时间和括号中的数字:

var myString = "10:30 am (15 left)11:00 am (15 left)11:30 am";
var pattern = /(\d{1,2}:\d{1,2}\s?[ap]m)\s\((\d+)/gi;
var match;
while (match = pattern.exec(myString)){
  console.log('Match: "' + match[0] + '" first group: -> "' + match[1] + '" second group -> ' + match[2]);
}

输出将是:

Match: "10:30 am (15" first group: -> "10:30 am" second group -> 15
Match: "11:00 am (15" first group: -> "11:00 am" second group -> 15

A better equivalent of preg_match_all from PHP in JS would be to use the exec() function. This will allow you to capture groups as well, with match() you can not do that.

For example you want to capture all times and the number in brackets from the variable myString:

var myString = "10:30 am (15 left)11:00 am (15 left)11:30 am";
var pattern = /(\d{1,2}:\d{1,2}\s?[ap]m)\s\((\d+)/gi;
var match;
while (match = pattern.exec(myString)){
  console.log('Match: "' + match[0] + '" first group: -> "' + match[1] + '" second group -> ' + match[2]);
}

The output will be:

Match: "10:30 am (15" first group: -> "10:30 am" second group -> 15
Match: "11:00 am (15" first group: -> "11:00 am" second group -> 15
许仙没带伞 2024-07-31 07:59:37

如果您需要与 php.ini 中一样的精确返回结构。
使用前请自行检查)

解决方案(在打字稿上):

function preg_match_all(regex: RegExp, str: string) {
  return [...str.matchAll(new RegExp(regex, 'g'))].reduce((acc, group) => {
    group.filter((element) => typeof element === 'string').forEach((element, i) => {
      if (!acc[i]) acc[i] = [];
      acc[i].push(element);
    });

    return acc;
  }, [] as string[][]);
}

示例(在javascript上):

const str = ';9N;N1;CP-S2;EU;CP-S3;E;CP-S4;KRT;VH;;VL;CP-S2;CP-S3;CP-S4';
const regex = /CP-(S[\d*])/;
const result = preg_match_all(regex, str);

console.log(result);

function preg_match_all(regex, str) {
  return [...str.matchAll(new RegExp(regex, 'g'))].reduce((acc, group) => {
    group.filter((element) => typeof element === 'string').forEach((element, i) => {
      if (!acc[i]) acc[i] = [];
      acc[i].push(element);
    });

    return acc;
  }, []);
}

If you need the exactly return structure as in php.
Please check for yourself before usage)

Solution (on typescript):

function preg_match_all(regex: RegExp, str: string) {
  return [...str.matchAll(new RegExp(regex, 'g'))].reduce((acc, group) => {
    group.filter((element) => typeof element === 'string').forEach((element, i) => {
      if (!acc[i]) acc[i] = [];
      acc[i].push(element);
    });

    return acc;
  }, [] as string[][]);
}

Example (on javascript):

const str = ';9N;N1;CP-S2;EU;CP-S3;E;CP-S4;KRT;VH;;VL;CP-S2;CP-S3;CP-S4';
const regex = /CP-(S[\d*])/;
const result = preg_match_all(regex, str);

console.log(result);

function preg_match_all(regex, str) {
  return [...str.matchAll(new RegExp(regex, 'g'))].reduce((acc, group) => {
    group.filter((element) => typeof element === 'string').forEach((element, i) => {
      if (!acc[i]) acc[i] = [];
      acc[i].push(element);
    });

    return acc;
  }, []);
}

北城挽邺 2024-07-31 07:59:37

使用 new RegExp().test()

function preg_match_all(regex, str) {
  return new RegExp(regex,'g').test(str)
}

Use new RegExp().test()

function preg_match_all(regex, str) {
  return new RegExp(regex,'g').test(str)
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文