我正在尝试从一个大字符串中提取一些数据,我想知道是否可以使用正则表达式。目前,我正在使用 javascript。例如:
This is some [example] text for my javascript [regexp] [lack] of knowledge.
使用这个字符串,我想生成一个 JSON 数组,其中包含方括号之间的文本。
example, regexp, lack
我希望有人能帮助我以简单的方式做到这一点,以便我能够理解它是如何工作的。预先感谢您的帮助。丹尼尔!
最佳答案
var str = "This is some [example] text for my javascript [regexp] [lack] of knowledge."
var regex = /\[(.*?)\]/g, result, indices = [];
while ( (result = regex.exec(str)) ) {
indices.push(result[1]);
}
关于JavaScript 使用正则表达式进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22240485/