在javascript中从字符串中提取半结构化信息
Extract semi-structured information from a string in javascript
我有这样的句子:
"[Paris:location]和[Lyon:location]在法国"
我需要从中提取所有标记的部分("Paris:location"answers"Lyon:location")。
我已经尝试过使用正则表达式(RegExp
)的代码:
var regexEntity = new RegExp(''[.+:.+']', 'g');
var text = '[Paris:location] and [Lyon:location] are in France';
while ((match = regexEntity.exec(text))) {
console.log(match);
}
但这是我得到的输出,好像它正在检测冒号:
[ ':',
index: 6,
input: '[Paris:location] and [Lyon:location] are in France' ]
[ ':',
index: 26,
input: '[Paris:location] and [Lyon:location] are in France' ]
我的正则表达式有问题吗?你还会用其他方法获取这些信息吗?
.+
是贪婪的,你需要使用它的惰性版本:.+?
.
那么,就像这样简单:
var text = '[Paris:location] and [Lyon:location] are in France';
console.log(text.match(/'[.+?:.+?']/g));
您可以使用非延迟搜索和正向查找的正则表达式。
var regex = /'[(.*?)(?=:location)/gi,
string = '"[Paris:location] and [Lyon:location] are in France"',
match;
while ((match = regex.exec(string)) !== null) {
console.log(match[1]);
}
相关文章:
- ES6非结构化中的计算属性-非结构化整体对象
- 如何解析文本区域中的结构化字符串数据(接近JSON)以检索其所需的属性
- 从knockout.js中的另一个对象更新一个深度结构化的javascript对象
- 结构化和绑定(ajax响应)
- 从字符串行创建结构化列表
- 从 JSON 对象生成结构化 HTML
- 使用角度将结构化数据保存在火库中
- 使用 JS-ctypes 将结构化数据传递给本机代码
- 在javascript中存储没有数据库的结构化数据
- 复杂的结构化 JSON 迭代
- 将结构化字符串转换为数组
- 将结构化的反应式数据发布到客户端(在数据库集合之外)
- 从几乎相同的dom结构中提取内部文本
- 高级结构化:环绕ng重复项目Angular.js
- 结构化功能.可以't在现有代码中添加if语句
- Javascript切片在相同的结构化数组上做不同的事情
- 结构化克隆算法与深度复制算法有何不同?
- 从字符串创建结构化对象的JavaScript函数
- 在JS数组中推送带有变量的结构化字符串
- 在javascript中从字符串中提取半结构化信息