在javascript正则表达式中获取花括号之间的内容 [英] Getting content between curly braces in javascript regex

查看:1381
本文介绍了在javascript正则表达式中获取花括号之间的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用javascript获取花括号之间的内容,我找到了这个帖子:

I am trying to get content between curly braces with javascript, I found this thread:

正则表达式在花括号之间获取字符串{我想要在花括号之间}" ;

但我不知道如何应用像'/ {([^}] +)} /'

But I do not know how to apply a regex like '/{([^}]+)}/'

我试过过string.replace('/ {([^}] +)} /','');

I have tried string.replace('/{([^}]+)}/','');

但是这不起作用。

推荐答案

以下是一个使用示例:

var found = [],          // an array to collect the strings that are found
    rxp = /{([^}]+)}/g,
    str = "a {string} with {curly} braces",
    curMatch;

while( curMatch = rxp.exec( str ) ) {
    found.push( curMatch[1] );
}

console.log( found );    // ["string", "curly"]

这篇关于在javascript正则表达式中获取花括号之间的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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