【问题标题】:How to find multiple values within curly braces from a string如何从字符串中的花括号中查找多个值
【发布时间】:2016-01-15 06:50:32
【问题描述】:

如何使用 JavaScript 从字符串中查找大括号内的多个值。 例如

string = https://test.com/tour/reception/@{name1}/@{name2}/test/@{name3};

我试过这个,/@{[A-Za-z0-9À-ÿ_ .-]*}/,但不知道如何获得匹配的值。

如何从字符串中获取name1name2name3

【问题讨论】:

  • 你需要一个正则表达式。你试过什么吗?
  • 是的,我试过这个,/@{[A-Za-z0-9À-ÿ_ .-]*}/;但不知道如何获得匹配的值。

标签: javascript


【解决方案1】:

使用正则表达式匹配用大括号括起来的文本。您需要使用+? 进行非贪婪匹配。

var string = "https://test.com/tour/reception/@{name1}/@{name2}/test/@{name3}";
var matches = string.match(/{.+?}/g);

现在matches["{name1}", "{name2}", "{name3}"]

【讨论】:

  • and optionaly then matches.map(function(match){ return match.replace(/{(.*?)}/,"$1")}),以便仅获取令牌
  • @Gael 不,这不是必需的,而不是 match 使用 exec。见我的answer
【解决方案2】:

你可以使用这个正则表达式。

/{.*?}/g

例如

var s = 'https://test.com/tour/reception/@{name1}/@{name2}/test/@{name3};';
var res = s.match(/{.*?}/g);
for (var r in res)
{
  console.log(res[r]);
}

这个输出

{name1}
{name2}
{name3}

【讨论】:

  • 我写了正则表达式,但想知道如何获取所有匹配的值。?
  • 只需在 JS 中使用 match 函数,如下所示:matches = your_string_name.match(/{.*?}/g);matches 变量将保存所有值
【解决方案3】:

虽然其他答案显示了如何使用大括号提取文本,但没有一个答案真正回答了这个问题。

如何从字符串中获取name1name2name3

您可以使用String#indexOfString#substring 方法。

var str = "https://test.com/tour/reception/@{name1}/@{name2}/test/@{name3};";

// Copy the string
var copy = str;
var index,
  matches = []; // To store results

// Till there is string wrapped in curly braces
while ((index = copy.indexOf('{')) !== -1) {
  var closingIndex = copy.indexOf('}');
  matches.push(copy.substring(index + 1, closingIndex));

  // Update the string to remove the first match
  copy = copy.substring(closingIndex + 1);
}

console.log(matches);
document.body.innerHTML = '<pre>' + JSON.stringify(matches, 0, 4) + '</pre>';

使用正则表达式

/{([^}]*)}/

带有全局标志。

Regex101 Demo

正则表达式解释:

  1. {:从字面上匹配 {
  2. ([^}]*):匹配任何不是}的任意次数,并将匹配结果放入第一个捕获的组中。
  3. }:匹配右括号}
  4. g:全局标志

在 JavaScript 中,使用 RegExp#exec 获取结果。

var regex = /{([^}]*)}/g;

var str = "https://test.com/tour/reception/@{name1}/@{name2}/test/@{name3};";
var matches = [];

while (match = regex.exec(str)) {
  // match[1] is the first captured group
  matches.push(match[1]);
}

console.log(matches);
document.body.innerHTML = '<pre>' + JSON.stringify(matches, 0, 4) + '</pre>';

【讨论】:

    【解决方案4】:

    如果您想要非正则表达式语法(也许是为了更清楚):

    var str = "https://test.com/tour/reception/@{name1}/@{name2}/test/@{name3}";
    var bopen = false;
    var valIndex = {
      istart: 0,
      iend: 0
    };
    values = [];
    for (var i = 0 ; i < str.length ; i++) {
      var ch = str.charAt(i);
      if (ch == '{') {
        bopen = true;
        valIndex.istart = i;
    }
      if (ch == '}' && bopen == true){
        bopen = false;
        valIndex.iend = i;
        values.push(str.substring(valIndex.istart + 1, valIndex.iend));
      }
    }
    alert(values);
    

    警报结果是一个字符串数组:name1,name2,name3

    这是JSFiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 2019-07-29
      • 2010-12-16
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多