使用正则表达式在JavaScript中的花括号之间获取内容 [英] Getting content between curly braces in JavaScript with regex
本文介绍了使用正则表达式在JavaScript中的花括号之间获取内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用JavaScript在花括号之间获取内容.我找到了这个线程:正则表达式以在花括号之间添加字符串"{我想要花括号之间的字符串}"
I am trying to get content between curly braces with JavaScript. I found this thread: Regex to get string between curly braces "{I want what's between the curly braces}"
但是我不知道如何应用正则表达式,例如/\ {([^}] +)\}/
But I do not know how to apply a regex like /\{([^}]+)\}/
我已经尝试过 string.replace('/\ {([^}] +)\}/','');
,但这是行不通的.
I have tried string.replace('/\{([^}]+)\}/','');
, however this does not work.
推荐答案
以下是使用示例:
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屋!
查看全文