LMD*_*LMD 1 javascript regex string performance regex-group
我正在做一些字符串解析并想使用正则表达式。我正在迭代字符串,并希望使用正则表达式和偏移量应用诸如“startsWith”之类的内容,如果找到则返回匹配项,否则返回 null。在伪 JavaScript 中:
function startsWith(string, regex, offset) {
if (regex_matches_at_offset) {
return match;
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
一个简单直接的解决方案是先应用子字符串,然后匹配。但我想要使用正则表达式的“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;
}
}
Run Code Online (Sandbox Code Playgroud)
但对于正则表达式,我当前的解决方案(仅用于测试目的)如下所示:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这不是很令人满意,因为存在明显的问题(复制大部分字符串,执行正则表达式搜索两次......
预期的 :
例子 :
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
Run Code Online (Sandbox Code Playgroud)
一种方法是基于作为字符串传递的偏移量和模式构建正则表达式
.匹配偏移量以内的任何字符let startsWith = (str, reg, offset) =>{
let regex = `^.{${0,offset}}(${reg})`
let final = new RegExp(regex,'i')
let found = str.match(final)
console.log(found ? found[1] : found)
}
startsWith("Hello world !", 'h', 0); // "H"
startsWith("Hello world !", 'wor?', 6); // "wor"
startsWith("Hello world !", 'wor?', 10); // null
startsWith("Hello world !", 'z', 0); // nullRun Code Online (Sandbox Code Playgroud)
正如@mark所提到的,如果我们希望将正则表达式作为参数传递给函数,我们可以使用source属性
let startsWith = (str, reg, offset) =>{
let regex = `^.{${0,offset}}(${reg.source})`
let final = new RegExp(regex,'i')
let found = str.match(final)
console.log(found ? found[1] : found)
}
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); // nullRun Code Online (Sandbox Code Playgroud)