【发布时间】:2019-05-27 16:44:54
【问题描述】:
我正在做一些字符串解析并想使用正则表达式。我正在遍历字符串,并希望使用正则表达式和偏移量应用类似“startsWith”的内容,如果找到则返回匹配项,否则返回 null。在伪javascript中:
function startsWith(string, regex, offset) {
if (regex_matches_at_offset) {
return match;
} else {
return null;
}
}
一个简单直接的解决方案是先应用子字符串然后匹配。但我想要使用正则表达式的“startsWith”之类的东西。
如果是字符串而不是正则表达式,我会选择startsWith
function startsWith(string, other_string, offset) {
let starts_with=s.startsWith(other_string, offset); // startsWith from position offset onwards, as other_string has fixed length the "match" is also known
if (starts_with) {
return other_string; // other_string = match
} else {
return null;
}
}
但是对于正则表达式,我当前的解决方案(仅用于测试目的)看起来像:
function startsWith(string, regex, offset) {
let end_part=s.substring(offset); // Substring, performance issue
let match=end_part.match(regex); // Match it as we need the match
if (match.length === 0) {
return null;
} else {
match=match[0]; // Only care about 1st match
}
if (end_part.startsWith(match)) { // Check if match already starts at first position
return match;
} else {
return null;
}
}
这不是很令人满意,因为存在明显的问题(复制大部分字符串,执行两次正则表达式搜索......
预期:
- 从给定位置开始在字符串中查找正则表达式,不仅是布尔值,还要匹配
- 高性能(例如,不要复制整个字符串)
例子:
startsWith("Hello world !", /h/i, 0); // "H"
startsWith("Hello world !", /wor?/i, 6); // "wor"
startsWith("Hello world !", /wor?/i, 10); // null
startsWith("Hello world !", /z/i, 0); // null
【问题讨论】:
标签: javascript regex string performance regex-group